Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf4d2ba1

Browse files
solves reverse linked list ii
1 parent2e8e399 commitf4d2ba1

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-
publicstaticvoidmain(String[]args) {
18-
ListNodehead =newListNode(1);
19-
head.next =newListNode(2);
20-
head.next.next =newListNode(3);
21-
head.next.next.next =newListNode(4);
22-
head.next.next.next.next =newListNode(5);
23-
System.out.println(reverseBetween(head,2,4));
24-
}
17+
publicListNodereverseBetween(ListNodehead,intm,intn) {
18+
if (head ==null ||m ==n) {
19+
returnhead;
20+
}
2521

26-
publicstaticListNodereverseBetween(ListNodehead,intleftVal,intrightVal) {
27-
if (leftVal ==rightVal)returnhead;
22+
// Move the two pointers until they reach the proper starting point
23+
// in the list.
24+
ListNodecur =head,prev =null;
25+
while (m >1) {
26+
prev =cur;
27+
cur =cur.next;
28+
m--;
29+
n--;
30+
}
2831

29-
ListNodesentinelHead =newListNode(-1000);
30-
sentinelHead.next =head;
31-
ListNodebeforeLeft =getNodeBefore(sentinelHead,leftVal);
32-
System.out.println(beforeLeft.val);
33-
ListNodeleft =beforeLeft.next;
34-
ListNodea =beforeLeft.next,b =a.next,c =b.next;
32+
// The two pointers that will fix the final connections.
33+
ListNodecon =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+
ListNodethird =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-
returnsentinelHead.next;
47-
}
4844

49-
privatestaticListNodegetNodeBefore(ListNodehead,intvalue) {
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
returnhead;
5454
}
5555
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp