Skip to content

Commit d029ab7

Browse files
committed
review-5
1 parent 3ef7837 commit d029ab7

10 files changed

Lines changed: 172 additions & 75 deletions

File tree

‎src/main/java/com/chen/algorithm/znn/linkedlist/test19/Solution.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @Auther: zhunn
1717
* @Date: 2020/10/22 14:51
18-
* @Description: 删除链表的倒数第N个节点 1、遍历length-n+1;2、栈、3、双指针法
18+
* @Description: 删除链表的倒数第N个节点 1、遍历length-n+1;2、栈、3、双指针法(推荐)
1919
*/
2020
public class Solution {
2121

@@ -70,7 +70,7 @@ public ListNode removeNthFromEnd2(ListNode head, int n) {
7070
}
7171

7272
/**
73-
* 双指针法
73+
* 双指针法 (推荐)
7474
*
7575
* @param head 头结点
7676
* @param n 删除倒数第几个

‎src/main/java/com/chen/algorithm/znn/linkedlist/test206/Solution.java‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
*
1313
* @Auther: zhunn
1414
* @Date: 2020/10/22 17:23
15-
* @Description: 反转链表:1-迭代法,2-递归
15+
* @Description: 反转链表:1-双指针法(推荐),2-递归
1616
*/
1717
public class Solution {
1818

19-
public ListNode reverseList1(ListNode head) {
19+
public ListNode reverseList(ListNode head) {
2020
if (head == null || head.next == null) {
2121
return head;
2222
}
@@ -25,8 +25,8 @@ public ListNode reverseList1(ListNode head) {
2525
ListNode curr = head;
2626
while (curr != null) {
2727
ListNode nextTemp = curr.next;
28-
curr.next = pre;
29-
pre = curr;
28+
curr.next = pre; // 当前指针的next指向前一个,链表指针反转
29+
pre = curr; // pre和curr指针向后推进
3030
curr = nextTemp;
3131
}
3232

@@ -50,11 +50,12 @@ public void test() {
5050
ListNode l1_2 = new ListNode(6, l1_3);
5151
ListNode l1_1 = new ListNode(7, l1_2);
5252

53-
ListNode result = reverseList2(l1_1);
54-
System.out.println(result.val);
55-
System.out.println(result.next.val);
56-
System.out.println(result.next.next.val);
57-
System.out.println(result.next.next.next.val);
53+
ListNode result = reverseList(l1_1);
54+
ListNode a = result;
55+
while (a != null) {
56+
System.out.println(a.val);
57+
a = a.next;
58+
}
5859
}
5960

6061

‎src/main/java/com/chen/algorithm/znn/linkedlist/test21/Solution.java‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,23 @@
1717
public class Solution {
1818

1919
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
20-
21-
22-
ListNode prehead = new ListNode(0);
23-
24-
ListNode prev = prehead;
20+
ListNode res = new ListNode(0);
21+
ListNode curr = res;
2522

2623
while (l1 != null && l2 != null) {
2724

2825
if (l1.val <= l2.val) {
29-
prev.next = l1;
26+
curr.next = l1;
3027
l1 = l1.next;
3128
} else {
32-
prev.next = l2;
29+
curr.next = l2;
3330
l2 = l2.next;
3431
}
35-
36-
prev = prev.next;
32+
curr = curr.next;
3733
}
34+
curr.next = l1 == null ? l2 : l1;
3835

39-
prev.next = l1 == null ? l2 : l1;
40-
41-
return prehead.next;
36+
return res.next;
4237
}
4338

4439
@Test
@@ -71,5 +66,4 @@ public void testCase() {
7166

7267
}
7368

74-
7569
}

‎src/main/java/com/chen/algorithm/znn/linkedlist/test24/Solution.java‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @Auther: zhunn
2121
* @Date: 2020/10/23 10:48
22-
* @Description: 两两交换链表中结点:1-递归,2-迭代
22+
* @Description: 两两交换链表中结点:1-递归,2-迭代(推荐)
2323
*/
2424
public class Solution {
2525

@@ -41,7 +41,7 @@ public ListNode swapPairs1(ListNode head) {
4141
}
4242

4343
/**
44-
* 2-迭代
44+
* 2-迭代(推荐)
4545
*
4646
* @param head
4747
* @return
@@ -59,11 +59,11 @@ public ListNode swapPairs2(ListNode head) {
5959
ListNode node1 = temp.next;
6060
ListNode node2 = temp.next.next;
6161

62-
temp.next = node2;
6362
node1.next = node2.next;
64-
node2.next = node1;
63+
node2.next = temp.next; //等价于 node2.next = node1;
64+
temp.next = node2; //两两交换 画图演示,想着图写代码
6565

66-
temp = node1;
66+
temp = node1; // 临时结点往前推进
6767
}
6868
return dummyHead.next;
6969
}
@@ -87,9 +87,9 @@ public ListNode swapPairs3(ListNode head) {
8787
ListNode node1 = head;
8888
ListNode node2 = head.next;
8989

90-
temp.next = node2;
9190
node1.next = node2.next;
92-
node2.next = node1;
91+
node2.next = temp.next;
92+
temp.next = node2;
9393

9494
temp = node1;
9595
head = node1.next;
@@ -110,12 +110,12 @@ public void test() {
110110
l1_3.next = l1_4;
111111
l1_4.next = l1_5;
112112

113-
ListNode result = swapPairs3(l1_1);
113+
ListNode result = swapPairs2(l1_1);
114114

115-
System.out.println(result.val);
116-
System.out.println(result.next.val);
117-
System.out.println(result.next.next.val);
118-
System.out.println(result.next.next.next.val);
119-
System.out.println(result.next.next.next.next.val);
115+
ListNode a = result;
116+
while (a != null) {
117+
System.out.println(a.val);
118+
a = a.next;
119+
}
120120
}
121121
}

‎src/main/java/com/chen/algorithm/znn/linkedlist/test25/Solution.java‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.chen.algorithm.znn.linkedlist.test25;
22

33
import com.chen.algorithm.znn.linkedlist.ListNode;
4+
import org.junit.Test;
45

56
/**
7+
* https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/tu-jie-kge-yi-zu-fan-zhuan-lian-biao-by-user7208t/
68
* 25. K 个一组翻转链表
79
* 给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
810
* k 是一个正整数,它的值小于或等于链表的长度。
@@ -14,7 +16,7 @@
1416
*
1517
* @Auther: zhunn
1618
* @Date: 2020/10/24 17:23
17-
* @Description: K个一组翻转链表
19+
* @Description: K个一组翻转链表:1-官方解法;2-(推荐)链表分区(已翻转,待翻转,未翻转),pre,end,start=pre.next,nextTemp=end.next指针,pre和end移动指针到翻转后的start位置
1820
*/
1921
public class Solution {
2022

@@ -66,9 +68,18 @@ private ListNode[] myReverse(ListNode head, ListNode tail) {
6668
}
6769

6870

71+
/**
72+
* 链表分区,pre,end,start,nextTemp指针(推荐)
73+
* @param head
74+
* @param k
75+
* @return
76+
*/
6977
public ListNode reverseKGroup1(ListNode head, int k) {
70-
ListNode dummy = new ListNode(0);
71-
dummy.next = head;
78+
if (head == null || head.next == null) {
79+
return head;
80+
}
81+
82+
ListNode dummy = new ListNode(0, head);
7283

7384
ListNode pre = dummy;
7485
ListNode end = dummy;
@@ -80,13 +91,14 @@ public ListNode reverseKGroup1(ListNode head, int k) {
8091
if (end == null) {
8192
break;
8293
}
94+
8395
ListNode start = pre.next;
84-
ListNode next = end.next;
96+
ListNode nextTemp = end.next;
8597
end.next = null;
8698
pre.next = reverse(start);
87-
start.next = next;
88-
pre = start;
99+
start.next = nextTemp;
89100

101+
pre = start;
90102
end = pre;
91103
}
92104
return dummy.next;
@@ -96,13 +108,35 @@ private ListNode reverse(ListNode head) {
96108
ListNode pre = null;
97109
ListNode curr = head;
98110
while (curr != null) {
99-
ListNode next = curr.next;
111+
ListNode nextTemp = curr.next;
100112
curr.next = pre;
113+
101114
pre = curr;
102-
curr = next;
115+
curr = nextTemp;
103116
}
104117
return pre;
105118
}
106119

120+
@Test
121+
public void test() {
122+
ListNode l1_1 = new ListNode(1);
123+
ListNode l1_2 = new ListNode(2);
124+
ListNode l1_3 = new ListNode(3);
125+
ListNode l1_4 = new ListNode(4);
126+
ListNode l1_5 = new ListNode(5);
127+
128+
l1_1.next = l1_2;
129+
l1_2.next = l1_3;
130+
l1_3.next = l1_4;
131+
l1_4.next = l1_5;
132+
133+
ListNode result = reverseKGroup1(l1_1, 3);
134+
135+
ListNode a = result;
136+
while (a != null) {
137+
System.out.println(a.val);
138+
a = a.next;
139+
}
140+
}
107141

108142
}

‎src/main/java/com/chen/algorithm/znn/linkedlist/test83/Solution.java‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.chen.algorithm.znn.linkedlist.test83;
22

33
import com.chen.algorithm.znn.linkedlist.ListNode;
4+
import org.junit.Test;
45

56
/**
67
* 83. 删除排序链表中的重复元素
@@ -14,7 +15,7 @@
1415
*
1516
* @Author: zhunn
1617
* @Date: 2020-10-08 02:19
17-
* @Description: 删除排序链表中的重复元素
18+
* @Description: 删除排序链表中的重复元素:同删除排序数组中的重复元素
1819
*/
1920
public class Solution {
2021

@@ -44,5 +45,22 @@ public ListNode deleteDuplicates(ListNode head) {
4445
return head;
4546
}
4647

48+
@Test
49+
public void test() {
50+
ListNode five = new ListNode(3);
51+
ListNode four = new ListNode(3, five);
52+
ListNode three = new ListNode(2, four);
53+
ListNode two = new ListNode(1, three);
54+
ListNode head = new ListNode(1, two);
55+
56+
ListNode result = deleteDuplicates(head);
57+
58+
ListNode a = result;
59+
while (a != null) {
60+
System.out.println(a.val);
61+
a = a.next;
62+
}
63+
//System.out.println(result);
64+
}
4765

4866
}

‎src/main/java/com/chen/algorithm/znn/linkedlist/test92/Solution.java‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.chen.algorithm.znn.linkedlist.test92;
22

33
import com.chen.algorithm.znn.linkedlist.ListNode;
4+
import com.chen.api.util.thread.study.chapter2.t6.CommonUtils;
45
import org.junit.Test;
56

7+
import javax.sound.midi.Soundbank;
8+
69
/**
10+
* https://leetcode-cn.com/problems/reverse-linked-list-ii/solution/java-shuang-zhi-zhen-tou-cha-fa-by-mu-yi-cheng-zho/
711
* 92. 反转链表 II
812
* 反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
913
* 说明:
@@ -14,7 +18,7 @@
1418
*
1519
* @Auther: zhunn
1620
* @Date: 2020/10/24 17:23
17-
* @Description: 反转链表二:1-双指针,2-删除结点递推
21+
* @Description: 反转链表二:1-双指针,2-双指针头插法-删除结点递推(推荐)
1822
*/
1923
public class Solution {
2024

@@ -63,31 +67,34 @@ public ListNode reverseBetween1(ListNode head, int m, int n) {
6367
}
6468

6569
/**
66-
* 2-删除结点递推
70+
* 2-删除结点递推 (推荐)
71+
* curr后面的元素删除,然后添加到pre的后面。也即头插法
6772
*
6873
* @param head
6974
* @param m
7075
* @param n
7176
* @return
7277
*/
73-
public ListNode reverseBetween2(ListNode head, int m, int n) {
78+
public ListNode reverseBetween(ListNode head, int m, int n) {
7479
if (head == null || head.next == null) {
7580
return head;
7681
}
7782

78-
ListNode dummy = new ListNode(-1);
79-
dummy.next = head;
83+
ListNode dummy = new ListNode(-1, head);
8084
ListNode pre = dummy;
85+
ListNode curr = dummy.next;
86+
8187
for (int i = 0; i < m - 1; i++) {
8288
pre = pre.next;
83-
head = head.next;
89+
curr = curr.next; //将curr移动到第一个要反转的节点的位置上
8490
}
8591

8692
for (int i = m; i < n; i++) {
87-
ListNode nextTemp = head.next;
88-
head.next = nextTemp.next;
89-
nextTemp.next = pre.next;
90-
pre.next = nextTemp;
93+
ListNode removeNode = curr.next;
94+
curr.next = curr.next.next; // 删除head后面的元素
95+
96+
removeNode.next = pre.next; // head后面的元素添加到pre后面
97+
pre.next = removeNode;
9198
}
9299
return dummy.next;
93100
}
@@ -109,14 +116,13 @@ public void test() {
109116
l1_5.next = l1_6;
110117
l1_6.next = l1_7;
111118

112-
ListNode result = reverseBetween1(l1_1, 3, 6);
119+
ListNode result = reverseBetween(l1_1, 3, 6);
113120

114-
System.out.println(result.val);
115-
System.out.println(result.next.val);
116-
System.out.println(result.next.next.val);
117-
System.out.println(result.next.next.next.val);
118-
System.out.println(result.next.next.next.next.val);
119-
System.out.println(result.next.next.next.next.next.val);
120-
System.out.println(result.next.next.next.next.next.next.val);
121+
ListNode a = result;
122+
while (a != null) {
123+
System.out.print(a.val + " ");
124+
a = a.next;
125+
}
126+
System.out.println();
121127
}
122128
}

0 commit comments

Comments
 (0)