-
Notifications
You must be signed in to change notification settings - Fork 64
/
ListNode.java
72 lines (58 loc) · 2.12 KB
/
ListNode.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
69
70
71
72
//Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution{
public ListNode reverseKGroup(ListNode head, int k){
//Edge cases when list is null or has less than one node
if(k <= 1 && head == null || head.next == null){
return head;
}
//Start by getting the size of the list
ListNode temp = head;
int size = 0;
while(temp != null){
size++;
temp = temp.next;
}
//Initialize curr, temp and newhead pointers to have access points while traversing the list
ListNode curr = head;
ListNode prev = null;
ListNode newHead = null;
//Run a loop to reverse k group. Only get in this group if size is greater than k
while(size >= k){
//Initiallize nodes to keep track during reverse process
ListNode last = prev;
ListNode newEnd = curr;
ListNode next = curr.next;
//loop k times to reverse the k group
for (int i = 0; curr != null && i < k; i++) {
curr.next = prev;
prev = curr;
curr = next;
//Check to move next if it is not equal to null
if(next != null){
next = next.next;
}
}
//Assign last node - node before the the beginning of the next sublist
if(last != null){
last.next = prev;
}else{
//Assign the newHead with prev
newHead = prev;
}
//Point the newEnd to the next node of the main list
newEnd.next = curr;
//Reduce the size of the list by k times to a void a infinite loop
size -= k;
//Once the end of a part is done, assign prev with newEnd
prev = newEnd;
}
return newHead;
}
}