Skip to content

Commit e68b7a7

Browse files
committed
review-linkedlist
1 parent d029ab7 commit e68b7a7

4 files changed

Lines changed: 47 additions & 9 deletions

File tree

‎src/main/java/com/chen/algorithm/znn/linkedlist/test141/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.test141;
22

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

56
import java.util.HashSet;
67
import java.util.Set;
@@ -38,7 +39,7 @@ public class Solution {
3839
* @param head
3940
* @return
4041
*/
41-
public boolean hasCycle1(ListNode head) {
42+
public boolean hasCycle(ListNode head) {
4243
if (head == null || head.next == null) {
4344
return false;
4445
}
@@ -98,4 +99,21 @@ public boolean hasCycle3(ListNode head) {
9899
return false;
99100
}
100101

102+
@Test
103+
public void test() {
104+
ListNode l1_1 = new ListNode(6);
105+
ListNode l1_2 = new ListNode(1);
106+
ListNode l1_3 = new ListNode(8);
107+
ListNode l1_4 = new ListNode(7);
108+
ListNode l1_5 = new ListNode(5);
109+
110+
l1_1.next = l1_2;
111+
l1_2.next = l1_3;
112+
l1_3.next = l1_4;
113+
l1_4.next = l1_5;
114+
l1_5.next = l1_3;
115+
116+
boolean result = hasCycle(l1_1);
117+
System.out.println(result);
118+
}
101119
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.chen.algorithm.znn.linkedlist.ListNode;
5+
import org.junit.Test;
56

67
import java.util.HashSet;
78
import java.util.Set;
@@ -29,7 +30,7 @@
2930
* @Auther: zhunn
3031
* @Date: 2020/10/23 16:10
3132
* @Description: 环形链表二,找出入环点
32-
* 方法:1-哈希表,2-快慢指针
33+
* 方法:1-哈希表,2-快慢指针(推荐)
3334
*/
3435
public class Solution {
3536

@@ -39,7 +40,7 @@ public class Solution {
3940
* @param head
4041
* @return
4142
*/
42-
public ListNode detectCycle1(ListNode head) {
43+
public ListNode detectCycle(ListNode head) {
4344
if (head == null || head.next == null) {
4445
return null;
4546
}
@@ -85,4 +86,21 @@ public ListNode detectCycle2(ListNode head) {
8586
return slow;
8687
}
8788

89+
@Test
90+
public void test() {
91+
ListNode l1_1 = new ListNode(6);
92+
ListNode l1_2 = new ListNode(2);
93+
ListNode l1_3 = new ListNode(8);
94+
ListNode l1_4 = new ListNode(7);
95+
ListNode l1_5 = new ListNode(5);
96+
97+
l1_1.next = l1_2;
98+
l1_2.next = l1_3;
99+
l1_3.next = l1_4;
100+
l1_4.next = l1_5;
101+
l1_5.next = l1_3;
102+
103+
ListNode result = detectCycle2(l1_1);
104+
System.out.println(result.val);
105+
}
88106
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.junit.Test;
55

66
/**
7+
* https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
8+
* https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/tu-jie-xiang-jiao-lian-biao-by-user7208t/
79
* 160. 相交链表
810
* 编写一个程序,找到两个单链表相交的起始节点。
911
* 如下面的两个链表:
@@ -24,7 +26,7 @@
2426
*
2527
* @Author: zhunn
2628
* @Date: 2020-10-22 17:40
27-
* @Description: 相交链表
29+
* @Description: 相交链表:双指针法
2830
*/
2931
public class Solution {
3032

@@ -40,12 +42,12 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
4042
a = a == null ? headB : a.next;
4143
b = b == null ? headA : b.next;
4244
}
43-
return b;
45+
return a;
4446
}
4547

4648
@Test
4749
public void test() {
48-
ListNode l1_1 = new ListNode(4);
50+
ListNode l1_1 = new ListNode(6);
4951
ListNode l1_2 = new ListNode(1);
5052
ListNode l1_3 = new ListNode(8);
5153
ListNode l1_4 = new ListNode(7);

‎src/main/java/com/chen/algorithm/znn/note‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ linkedlist
117117
206. 反转链表 ******* 声明双指针pre=null, curr = head;curr.next = pre(链表指针反转),最后返回pre
118118
92. 反转链表 II 指定长度反转 双指针头插法-删除结点递推,画图演示,想图写代码
119119
25 K 个一组翻转链表 ??? (推荐)链表分区(已翻转,待翻转,未翻转),pre,end,start=pre.next,nextTemp=end.next指针,pre和end移动指针到翻转后的start位置
120-
160 相交链表 ***
121-
141 环形链表 ***
122-
142 环形链表 II ***
120+
160 相交链表 *** 双指针法(a+c+b = b+c+a)a=链表1未相交部分,b=链表2未相交部分,c=两链表相交部分
121+
141 环形链表(判断链表是否有环) 1-哈希表;(推荐)2-双指针法-快慢指针 while(fast != null && fast.next != null)
122+
142 环形链表 II ***(找出入环点) 1-哈希表;(推荐)2-快慢指针
123123
//148. 排序链表
124124

125125
tree

0 commit comments

Comments
 (0)