chore(Java): add fold-linked-list (#266)

pull/275/head
Kartick Gulati 2021-04-28 19:26:17 +05:30 committed by GitHub
parent 56a12bc250
commit 194316cb60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 0 deletions

View File

@ -18,6 +18,7 @@
3. [Doubly](linked-lists/doubly.java) 3. [Doubly](linked-lists/doubly.java)
4. [Reverse](linked-lists/reverse.java) 4. [Reverse](linked-lists/reverse.java)
5. [Singly](linked-lists/singly.java) 5. [Singly](linked-lists/singly.java)
6. [Fold Linked List](linked-lists/fold-linked-list.java)
## Queues ## Queues

View File

@ -0,0 +1,92 @@
// Java implementation for folding a linked list
public static class Node {
int data;
Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
public static class LinkedList {
Node head;
Node tail;
int size;
public void display() {
for (Node temp = head; temp != null; temp = temp.next) {
System.out.print(temp.data + " ");
}
System.out.println();
}
public void addFirst(int val) {
Node temp = new Node(val);
temp.next = head;
head = temp;
if (size == 0) {
tail = temp;
}
size++;
}
void addLast(int val) {
Node temp = new Node(val);
temp.next = null;
if (size == 0) {
head = tail = temp;
} else {
tail.next = temp;
tail = temp;
}
size++;
}
Node fleft;
private void foldhelper(Node right , int floor){
if(right == null){
return;
}
foldhelper(right.next , floor+1);
if(floor>size/2){
Node val = fleft.next;
fleft.next = right;
right.next = val;
fleft = val;
}
else if(floor==size/2){
this.tail = right;
tail.next = null;
}
else{
// do nothing
}
}
public void fold() {
fleft = this.head;
foldhelper(this.head, 0);
}
}
//Driver Code
public static void main(String[] args) throws Exception {
LinkedList ll = new LinkedList();
int n = 6;
for (int i = 1; i <= n; i++) {
ll.addLast(i);
}
// LinkedList --> [1,2,3,4,5,6] for n = 6
System.out.print("Linked List before folding --> ");
ll.display();
ll.fold();
System.out.print("Linked List after folding --> ");
ll.display();
}
/*
Output:-
Linked List before folding --> 1 2 3 4 5 6
Linked List after folding --> 1 6 2 5 3 4
*/