forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseLinkedListInGroups.java
68 lines (60 loc) · 1.76 KB
/
ReverseLinkedListInGroups.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.rampatra.linkedlists;
import com.rampatra.base.SingleLinkedList;
import com.rampatra.base.SingleLinkedNode;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/29/15
* @time: 2:32 PM
*/
public class ReverseLinkedListInGroups {
/**
* Reverses the linked list in groups.
* <p>
* Example:
* <p>
* Inputs: 1->2->3->4->5->6->7->8 and k = 3
* Output: 3->2->1->6->5->4->8->7.
* <p>
* Inputs: 1->2->3->4->5->6->7->8 and k = 5
* Output: 5->4->3->2->1->8->7->6.
*
* @param node
* @param k
* @return
*/
public static <E extends Comparable<E>> SingleLinkedNode<E> reverseLinkedListInGroups(SingleLinkedNode<E> node, int k) {
SingleLinkedNode<E> curr = node, prev = null, next = null;
int i = 0;
// reverse the 'next' pointer of nodes with help of 3 pointers
while (curr != null && i < k) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
i++;
}
// recursively call for the rest of the nodes in the linked list
if (next != null) {
node.next = reverseLinkedListInGroups(curr, k);
}
return prev;
}
public static void main(String[] args) {
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
linkedList.add(0);
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);
linkedList.add(5);
linkedList.add(6);
linkedList.add(7);
linkedList.add(8);
linkedList.add(9);
linkedList.add(10);
linkedList.printList();
SingleLinkedList.printList(reverseLinkedListInGroups(linkedList.head, 3));
}
}