Skip to content

Commit

Permalink
linkedlist fold
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav12k committed Jun 7, 2020
1 parent a12abd7 commit a78a54f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
29 changes: 29 additions & 0 deletions LinkedList/LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,33 @@ private void ReverseDataRecursivelyHeap(Heapmover hp, Node right, int count) {
hp.left = hp.left.next;
}

public void fold() {
fold(head, head, 0);
}

private Node fold(Node left, Node right, int count) {

if (right == null) {
return left;
}

left = fold(left, right.next, count + 1);

if (count > size / 2) {
Node ahead = left.next;
left.next = right;
right.next = ahead;
return ahead;
}

if (count == size / 2) {

tail = right;
tail.next = null;

}

return null;
}

}
3 changes: 3 additions & 0 deletions LinkedList/LinkedListClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public static void main(String[] args) throws Exception {

ll.RDRHeap();
ll.display();

ll.fold();
ll.display();
}

}

0 comments on commit a78a54f

Please sign in to comment.