-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.java
83 lines (61 loc) · 2.47 KB
/
Solution.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
73
74
75
76
77
78
79
80
81
82
83
/*
@lc id : 25 Reverse Nodes in k-Group
@url : https://leetcode.com/problems/reverse-nodes-in-k-group/
@author : rohit
@date : 05/07/2020
*/
class Solution {
//This function reverse first k nodes in ll
public ListNode reverseKNodes(ListNode head, int k){
if(head == null || head.next == null || k == 1)
return head;
ListNode finalHead = reverseKNodes(head.next,k-1);
ListNode temp = head.next;
temp.next = head;
head.next = null;
return finalHead;
}
//Return kth Node
public ListNode getKthNode(ListNode head, int k){
ListNode kthNode = head;
for(int i = 1; i < k; i++){
if(kthNode == null)
return null;
kthNode = kthNode.next;
}
return kthNode;
}
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null || head.next == null || k == 0 || k ==1)
return head;
ListNode kthNode, nextOfKthNode;
ListNode finalHead = new ListNode(0);
//Tail of previous group
ListNode prevTail = finalHead;
while(head != null && head.next != null){
//This will be the tail of current group after reversing k nodes
ListNode currTail = head;
/*We need to find kthNode to ensure that k or more elements
are still left in list to reverse, if not, then reversal is complete and break*/
kthNode = getKthNode(head, k);
//Less than k nodes left, so break
if(kthNode == null)
break;
//Find first element of next group
nextOfKthNode = getKthNode(head, k+1);
//Get head of current group after reversing
ListNode currHead = reverseKNodes(head, k);
//Point tail of previous group to head of current group
prevTail.next = currHead;
//Point tail of current list to rest of the nodes in group
currTail.next = nextOfKthNode;
//Move head to first element of next group
head = nextOfKthNode;
//Now finally, since we have got a new group,
//now point prevTail to tail of current group
prevTail = currTail;
}
//Return next of dummy node
return finalHead.next;
}
}