You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ _If you like this project, please give me a star._ ★
4
4
5
5
> ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews)
6
6
7
-
###If you feel benefited from Leetcode and loves it, please consider to [donate to Leetcode](https://leetcode.com/donate/) in order to help us build the best OJ platform.
7
+
##If you feel benefited from Leetcode and loves it, please consider to [donate to Leetcode](https://leetcode.com/donate/) in order to help us build the best OJ platform.
8
8
9
9
## Contributing
10
10
Your ideas/fixes/algorithms are more than welcome!
* Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
8
-
9
-
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
10
-
11
-
You may not alter the values in the nodes, only nodes itself may be changed.
12
-
13
-
Only constant memory is allowed.
8
+
* k is a positive integer and is less than or equal to the length of the linked list.
9
+
* If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
10
+
* You may not alter the values in the nodes, only nodes itself may be changed.
11
+
* Only constant memory is allowed.
14
12
15
13
For example,
16
14
Given this linked list: 1->2->3->4->5
@@ -19,8 +17,11 @@
19
17
20
18
For k = 3, you should return: 3->2->1->4->5
21
19
*/
20
+
22
21
publicclass_25 {
23
22
23
+
/**We use recursion to go all the way until the end: when the number of nodes are smaller than k;
24
+
* then we start to reverse each group of k nodes from the end towards the start.*/
24
25
publicListNodereverseKGroup(ListNodehead, intk) {
25
26
ListNodecurr = head;
26
27
intcount = 0;
@@ -31,31 +32,21 @@ public ListNode reverseKGroup(ListNode head, int k) {
31
32
}
32
33
33
34
if (count == k) {
34
-
//if k+1 is found
35
-
curr = reverseKGroup(curr, k);//reverse list that has k+1 as head
35
+
/**after this below recursive call finishes, it'll return head;
36
+
* then this returned "head" will become "curr", while the head
37
+
* in its previous callstack is the real head after this call.
38
+
* Setting up a break point will make all of this crystal clear.*/
39
+
curr = reverseKGroup(curr, k);
36
40
37
-
while (count > 0) {
41
+
while (count-- > 0) {
38
42
ListNodetemp = head.next;
39
43
head.next = curr;
40
44
curr = head;
41
45
head = temp;
42
-
count--;
43
46
}
44
47
head = curr;
45
48
}
46
49
returnhead;//we run out of nodes before we hit count == k, so we'll just directly return head in this case as well
0 commit comments