Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f4d2ba1

Browse files
solves reverse linked list ii
1 parent 2e8e399 commit f4d2ba1

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
## Problems
1212
| # | Name | Solution | Youtube |
1313
|:----:|-----------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------------:|
14-
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [![Java](assets/java.png)](src/TwoSum.java) [![Python](assets/python.png)](python/two_sum.py) | [![java-yt](assets/java-yt.png)](https://youtu.be/9wSL_7NN-A8) [![python-yt](assets/python-yt.png)](https://youtu.be/N5FXCTg0TDE) |
14+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum) | [![Java](assets/java.png)](src/TwoSum.java) [![Python](assets/python.png)](python/two_sum.py) | [![java-yt](assets/java-yt.png)](https://youtu.be/9wSL_7NN-A8) [![python-yt](assets/python-yt.png)](https://youtu.be/N5FXCTg0TDE) |
1515
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers) | [![Java](assets/java.png)](src/AddTwoNumbers.java) | |
1616
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters) | [![Java](assets/java.png)](src/LongestSubstringWithoutRepeatingCharacters.java) | |
1717
| 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring) | [![Java](assets/java.png)](src/LongestPalindromicSubstring.java) | |
@@ -82,6 +82,7 @@
8282
| 89 | [Gray Code](https://leetcode.com/problems/gray-code) | [![Java](assets/java.png)](src/GrayCode.java) | |
8383
| 90 | [Subsets II](https://leetcode.com/problems/subsets-ii) | [![Java](assets/java.png)](src/SubsetsII.java) | |
8484
| 91 | [Decode Ways](https://leetcode.com/problems/decode-ways) | [![Java](assets/java.png)](src/DecodeWays.java) | |
85+
| 92 | [Reverse Lined](https://leetcode.com/problems/decode-ways) | [![Java](assets/java.png)](src/DecodeWays.java) | |
8586
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [![Java](assets/java.png)](src/BinaryTreeInorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_inorder_traversal.py) | |
8687
| 100 | [Same Tree](https://leetcode.com/problems/same-tree) | [![Java](assets/java.png)](src/SameTree.java) [![Python](assets/python.png)](python/same_tree.py) | |
8788
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree) | [![Java](assets/java.png)](src/SymmetricTree.java) [![Python](assets/python.png)](python/symmetric_tree.py) | |

src/ReverseLinkedListII.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,42 @@ public String toString() {
1414
}
1515
}
1616

17-
public static void main(String[] args) {
18-
ListNode head = new ListNode(1);
19-
head.next = new ListNode(2);
20-
head.next.next = new ListNode(3);
21-
head.next.next.next = new ListNode(4);
22-
head.next.next.next.next = new ListNode(5);
23-
System.out.println(reverseBetween(head, 2, 4));
24-
}
17+
public ListNode reverseBetween(ListNode head, int m, int n) {
18+
if (head == null || m == n) {
19+
return head;
20+
}
2521

26-
public static ListNode reverseBetween(ListNode head, int leftVal, int rightVal) {
27-
if (leftVal == rightVal) return head;
22+
// Move the two pointers until they reach the proper starting point
23+
// in the list.
24+
ListNode cur = head, prev = null;
25+
while (m > 1) {
26+
prev = cur;
27+
cur = cur.next;
28+
m--;
29+
n--;
30+
}
2831

29-
ListNode sentinelHead = new ListNode(-1000);
30-
sentinelHead.next = head;
31-
ListNode beforeLeft = getNodeBefore(sentinelHead, leftVal);
32-
System.out.println(beforeLeft.val);
33-
ListNode left = beforeLeft.next;
34-
ListNode a = beforeLeft.next, b = a.next, c = b.next;
32+
// The two pointers that will fix the final connections.
33+
ListNode con = prev, tail = cur;
3534

36-
while (c != null && b.val != rightVal) {
37-
a.next = null;
38-
b.next = a;
39-
a = b;
40-
b = c;
41-
c = c.next;
35+
// Iteratively reverse the nodes until n becomes 0.
36+
ListNode third = null;
37+
while (n > 0) {
38+
third = cur.next;
39+
cur.next = prev;
40+
prev = cur;
41+
cur = third;
42+
n--;
4243
}
43-
b.next = a;
44-
beforeLeft.next = b;
45-
left.next = c;
46-
return sentinelHead.next;
47-
}
4844

49-
private static ListNode getNodeBefore(ListNode head, int value) {
50-
while (head != null && head.next != null && head.next.val != value) {
51-
head = head.next;
45+
// Adjust the final connections
46+
if (con != null) {
47+
con.next = prev;
48+
} else {
49+
head = prev;
5250
}
51+
52+
tail.next = cur;
5353
return head;
5454
}
5555
}

0 commit comments

Comments
 (0)