Skip to content

Commit a3715bf

Browse files
solves #25: Reverse Nodes in k-Group in java
1 parent 9d46e88 commit a3715bf

File tree

5 files changed

+84
-64
lines changed

5 files changed

+84
-64
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
| 21 | [Merge 2 Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [![Java](assets/java.png)](src/Merge2SortedLists.java) [![Python](assets/python.png)](python/merge_2_sorted_lists.py) | |
3434
| 22 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses) | [![Java](assets/java.png)](src/GenerateParentheses.java) | |
3535
| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs) | [![Java](assets/java.png)](src/SwapNodesInPairs.java) | |
36+
| 25 | [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group) | [![Java](assets/java.png)](src/ReverseNodesInKGroup.java) | |
3637
| 26 | [Remove Duplicates From Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedArray.java) [![Python](assets/python.png)](python/remove_duplicates_from_sorted_array.py) [![js](assets/javascript.png)](javascript/RemoveDuplicatesFromSortedArray.js) | |
3738
| 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [![Java](assets/java.png)](src/RemoveElement.java) [![Python](assets/python.png)](python/remove_element.py) | |
3839
| 28 | [Needle in Haystack](https://leetcode.com/problems/implement-strstr) | [![Java](assets/java.png)](src/NeedleInHaystack.java) [![Python](assets/python.png)](python/needle_in_haystack.py) | |

src/CopyListWithRandomPointer.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.Map;
77

88
public class CopyListWithRandomPointer {
9-
private static final class Node {
9+
public static final class Node {
1010
int val;
1111
Node next;
1212
Node random;
@@ -18,7 +18,7 @@ public Node(int val) {
1818
}
1919
}
2020

21-
public HelloWorld.Node copyRandomList(HelloWorld.Node head) {
21+
public Node copyRandomList(Node head) {
2222
if (head == null) {
2323
return null;
2424
}
@@ -29,26 +29,26 @@ public HelloWorld.Node copyRandomList(HelloWorld.Node head) {
2929
}
3030

3131
// A->B->C --> A->A'->B->B'
32-
private static void createWeavedList(HelloWorld.Node head) {
33-
for (HelloWorld.Node i = head; i != null ; i = i.next.next) {
34-
HelloWorld.Node newNode = new HelloWorld.Node(i.val);
32+
private static void createWeavedList(Node head) {
33+
for (Node i = head; i != null ; i = i.next.next) {
34+
Node newNode = new Node(i.val);
3535
newNode.next = i.next;
3636
i.next = newNode;
3737
}
3838
}
3939

40-
private static void linkRandomPointersForNewNodes(HelloWorld.Node head) {
41-
for (HelloWorld.Node i = head; i != null ; i = i.next.next) {
40+
private static void linkRandomPointersForNewNodes(Node head) {
41+
for (Node i = head; i != null ; i = i.next.next) {
4242
if (i.random == null) {
4343
continue;
4444
}
4545
i.next.random = i.random.next;
4646
}
4747
}
4848

49-
private static HelloWorld.Node unweaveList(HelloWorld.Node head) {
50-
final HelloWorld.Node pointerNew = head.next;
51-
for (HelloWorld.Node old = head, i = head.next; i != null && old != null ; i = i.next, old = old.next) {
49+
private static Node unweaveList(Node head) {
50+
final Node pointerNew = head.next;
51+
for (Node old = head, i = head.next; i != null && old != null ; i = i.next, old = old.next) {
5252
old.next = old.next == null ? null : old.next.next;
5353
i.next = i.next == null ? null : i.next.next;
5454
}

src/HelloWorld.java

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,35 @@
11
public class HelloWorld {
2-
public static class Node {
3-
int val;
4-
Node next;
5-
Node random;
6-
7-
public Node(int val) {
8-
this.val = val;
9-
this.next = null;
10-
this.random = null;
11-
}
12-
}
13-
14-
/*
15-
// Definition for a Node.
16-
class Node {
17-
public int val;
18-
public Node next;
19-
public Node random;
20-
21-
public Node() {}
22-
23-
public Node(int _val,Node _next,Node _random) {
24-
val = _val;
25-
next = _next;
26-
random = _random;
27-
}
28-
};
29-
*/
30-
31-
public Node copyRandomList(Node head) {
2+
public ListNode reverseBetween(ListNode head, int left, int right) {
323
if (head == null) {
334
return null;
345
}
356

36-
createWeavedList(head);
37-
linkRandomPointersForNewNodes(head);
38-
return unweaveList(head);
39-
}
7+
ListNode result = new ListNode(0);
8+
result.next = head;
409

41-
// A->B->C --> A->A'->B->B'
42-
private static void createWeavedList(Node head) {
43-
for (Node i = head ; i != null ; i = i.next.next) {
44-
Node newNode = new Node(i.val);
45-
newNode.next = i.next;
46-
i.next = newNode;
10+
ListNode start = result;
11+
for (int i = 1 ; i < left ; i++) {
12+
start = start.next;
4713
}
48-
}
4914

50-
private static void linkRandomPointersForNewNodes(Node head) {
51-
for (Node i = head ; i != null ; i = i.next.next) {
52-
if (i.random == null) {
53-
continue;
54-
}
55-
i.next.random = i.random.next;
15+
ListNode startNext = start.next, a = startNext, b = start.next.next;
16+
for (int count = left ; b != null && count < right ; count++) {
17+
ListNode c = b.next;
18+
b.next = a;
19+
a = b;
20+
b = c;
5621
}
22+
23+
start.next = a;
24+
startNext.next = b;
25+
26+
return result.next;
5727
}
5828

59-
private static Node unweaveList(Node head) {
60-
final Node pointerNew = head.next;
61-
for (Node old = head, i = head.next ; i != null && old != null ; i = i.next, old = old.next) {
62-
old.next = old.next == null ? null : old.next.next;
63-
i.next = i.next == null ? null : i.next.next;
64-
}
65-
return pointerNew;
29+
public static void main(String[] args) {
30+
ListNode node = new ListNode(1);
31+
node.next = new ListNode(2);
32+
node.next.next = new ListNode(3);
33+
node.next.next.next = new ListNode(4);
6634
}
6735
}

src/ListNode.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ public class ListNode {
1212
this.val = val;
1313
this.next = next;
1414
}
15+
16+
@Override
17+
public String toString() {
18+
return "ListNode{" +
19+
"val=" + val +
20+
'}';
21+
}
1522
}

src/ReverseNodesInKGroup.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// https://leetcode.com/problems/reverse-nodes-in-k-group
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class ReverseNodesInKGroup {
6+
public static ListNode reverseKGroup(ListNode head, int k) {
7+
if (head == null || head.next == null || k == 1) {
8+
return head;
9+
}
10+
11+
final ListNode result = new ListNode(0);
12+
result.next = head;
13+
14+
for (ListNode i = result ; i != null ; ) {
15+
ListNode start = i;
16+
17+
// go forward k steps
18+
int count = 0;
19+
for ( ; count < k && i != null ; count++) {
20+
i = i.next;
21+
}
22+
23+
if (count == k && i != null) {
24+
i = reverse(start, i);
25+
}
26+
}
27+
28+
return result.next;
29+
}
30+
31+
private static ListNode reverse(ListNode start, ListNode end) {
32+
ListNode a = start.next, b = start.next.next, terminal = end.next;
33+
a.next = end.next;
34+
while (b != terminal) {
35+
ListNode c = b.next;
36+
b.next = a;
37+
a = b;
38+
b = c;
39+
}
40+
ListNode newStart = start.next;
41+
start.next = a;
42+
return newStart;
43+
}
44+
}

0 commit comments

Comments
 (0)