Skip to content

Commit 0d44125

Browse files
committed
链表其他解法
1 parent 1e951de commit 0d44125

File tree

7 files changed

+326
-6
lines changed

7 files changed

+326
-6
lines changed

src/main/java/com/chen/algorithm/study/test141/Solution1.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*
66
* @author : chen weijie
77
* @Date: 2019-11-02 15:58
8-
* @Description: zhunn 判断链表是否有环
98
*/
109
public class Solution1 {
1110

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.chen.algorithm.study.test141;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* @Auther: zhunn
8+
* @Date: 2020/10/23 16:26
9+
* @Description: 环形链表一:判断链表是否有环
10+
* 方法:1-哈希表,2-快慢指针
11+
*/
12+
public class Solution3 {
13+
class ListNode {
14+
int val;
15+
ListNode next;
16+
17+
ListNode(int x) {
18+
val = x;
19+
next = null;
20+
}
21+
}
22+
23+
24+
/**
25+
* 2-快慢指针
26+
* @param head
27+
* @return
28+
*/
29+
public boolean hasCycle1(ListNode head) {
30+
if (head == null || head.next == null) {
31+
return false;
32+
}
33+
34+
ListNode slow = head;
35+
ListNode fast = head;
36+
37+
while (fast != null && fast.next != null) {
38+
slow = slow.next;
39+
fast = fast.next.next;
40+
41+
if (slow == fast) {
42+
return true;
43+
}
44+
}
45+
return false;
46+
}
47+
48+
/**
49+
* 1-哈希表
50+
* @param head
51+
* @return
52+
*/
53+
public boolean hasCycle2(ListNode head) {
54+
if (head == null || head.next == null) {
55+
return false;
56+
}
57+
58+
ListNode dummy = head;
59+
Set<ListNode> visited = new HashSet<>();
60+
while (dummy != null) {
61+
if (visited.contains(dummy)) {
62+
return true;
63+
} else {
64+
visited.add(dummy);
65+
}
66+
dummy = dummy.next;
67+
}
68+
return false;
69+
}
70+
71+
/**
72+
* 1-哈希表简易版
73+
* @param head
74+
* @return
75+
*/
76+
public boolean hasCycle3(ListNode head) {
77+
Set<ListNode> seen = new HashSet<>();
78+
while (head != null) {
79+
if (!seen.add(head)) {
80+
return true;
81+
}
82+
head = head.next;
83+
}
84+
return false;
85+
}
86+
87+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.chen.algorithm.study.test142;
2+
3+
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
* @Auther: zhunn
9+
* @Date: 2020/10/23 16:10
10+
* @Description: 环形链表二,找出入环点
11+
* 方法:1-哈希表,2-快慢指针
12+
*/
13+
public class Solution4 {
14+
15+
/**
16+
* 1-哈希表
17+
* @param head
18+
* @return
19+
*/
20+
public ListNode detectCycle1(ListNode head) {
21+
if (head == null || head.next == null) {
22+
return null;
23+
}
24+
ListNode dummy = head;
25+
Set<ListNode> visited = new HashSet<>();
26+
while (dummy != null) {
27+
if (visited.contains(dummy)) {
28+
return dummy;
29+
} else {
30+
visited.add(dummy);
31+
}
32+
dummy = dummy.next;
33+
}
34+
return null;
35+
}
36+
37+
/**
38+
* 2-快慢指针
39+
* @param head
40+
* @return
41+
*/
42+
public ListNode detectCycle2(ListNode head) {
43+
if (head == null || head.next == null) {
44+
return null;
45+
}
46+
47+
ListNode slow = head;
48+
ListNode fast = head;
49+
while (fast != null && fast.next != null) {
50+
slow = slow.next;
51+
fast = fast.next.next;
52+
if (slow == fast) {
53+
break;
54+
}
55+
}
56+
57+
ListNode ptr = head;
58+
while (slow != ptr) {
59+
slow = slow.next;
60+
ptr = ptr.next;
61+
}
62+
return slow;
63+
}
64+
65+
}

src/main/java/com/chen/algorithm/study/test160/Solution.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.chen.algorithm.study.test160;
22

3+
import org.junit.Test;
4+
35
/**
46
* @author : chen weijie
57
* @Date: 2019-11-02 17:40
8+
* @Description: zhunn 相交链表
69
*/
710
public class Solution {
811

@@ -32,4 +35,33 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
3235
return b;
3336
}
3437

38+
@Test
39+
public void test() {
40+
ListNode l1_1 = new ListNode(4);
41+
ListNode l1_2 = new ListNode(1);
42+
ListNode l1_3 = new ListNode(8);
43+
ListNode l1_4 = new ListNode(7);
44+
ListNode l1_5 = new ListNode(5);
45+
46+
ListNode l2_1 = new ListNode(5);
47+
ListNode l2_2 = new ListNode(0);
48+
ListNode l2_3 = new ListNode(1);
49+
50+
l1_1.next = l1_2;
51+
l1_2.next = l1_3;
52+
l1_3.next = l1_4;
53+
l1_4.next = l1_5;
54+
55+
l2_1.next = l2_2;
56+
l2_2.next = l2_3;
57+
l2_3.next = l1_3;
58+
59+
ListNode result = getIntersectionNode(l1_1, l2_1);
60+
61+
while (result != null) {
62+
System.out.println(result.val);
63+
result = result.next;
64+
}
65+
}
66+
3567
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.chen.algorithm.study.test24;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @Auther: zhunn
7+
* @Date: 2020/10/23 10:48
8+
* @Description: 两两交换链表中结点:1-递归,2-迭代
9+
*/
10+
public class Solution3 {
11+
12+
13+
/**
14+
* 1-递归
15+
* @param head
16+
* @return
17+
*/
18+
public ListNode swapPairs1(ListNode head) {
19+
if (head == null || head.next == null) {
20+
return head;
21+
}
22+
ListNode newHead = head.next;
23+
head.next = swapPairs1(newHead.next);
24+
newHead.next = head;
25+
return newHead;
26+
}
27+
28+
/**
29+
* 2-迭代
30+
* @param head
31+
* @return
32+
*/
33+
public ListNode swapPairs2(ListNode head) {
34+
if (head == null || head.next == null) {
35+
return head;
36+
}
37+
38+
ListNode dummyHead = new ListNode(-1);
39+
dummyHead.next = head;
40+
ListNode temp = dummyHead;
41+
42+
while (temp.next != null && temp.next.next != null) {
43+
ListNode node1 = temp.next;
44+
ListNode node2 = temp.next.next;
45+
46+
temp.next = node2;
47+
node1.next = node2.next;
48+
node2.next = node1;
49+
50+
temp = node1;
51+
}
52+
return dummyHead.next;
53+
}
54+
55+
/**
56+
* 2-迭代(操作head)
57+
* @param head
58+
* @return
59+
*/
60+
public ListNode swapPairs3(ListNode head) {
61+
if (head == null || head.next == null) {
62+
return head;
63+
}
64+
65+
ListNode dummyHead = new ListNode(-1);
66+
dummyHead.next = head;
67+
ListNode temp = dummyHead;
68+
69+
while (head != null && head.next != null) {
70+
ListNode node1 = head;
71+
ListNode node2 = head.next;
72+
73+
temp.next = node2;
74+
node1.next = node2.next;
75+
node2.next = node1;
76+
77+
temp = node1;
78+
head = node1.next;
79+
}
80+
return dummyHead.next;
81+
}
82+
83+
@Test
84+
public void test() {
85+
ListNode l1_1 = new ListNode(1);
86+
ListNode l1_2 = new ListNode(2);
87+
ListNode l1_3 = new ListNode(3);
88+
ListNode l1_4 = new ListNode(4);
89+
ListNode l1_5 = new ListNode(5);
90+
91+
l1_1.next = l1_2;
92+
l1_2.next = l1_3;
93+
l1_3.next = l1_4;
94+
l1_4.next = l1_5;
95+
96+
ListNode result = swapPairs3(l1_1);
97+
98+
System.out.println(result.val);
99+
System.out.println(result.next.val);
100+
System.out.println(result.next.next.val);
101+
System.out.println(result.next.next.next.val);
102+
System.out.println(result.next.next.next.next.val);
103+
}
104+
}

src/main/java/com/chen/algorithm/study/test242/Solution.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
/**
66
* @author : chen weijie
77
* @Date: 2020-05-03 22:12
8+
* @Description: zhunn 有效的字母异位词。哈希表
89
*/
910
public class Solution {
1011

1112
public boolean isAnagram(String s, String t) {
1213

13-
if (s.length() != t.length()) {
14+
if (s == null || t == null || s.length() != t.length()) {
1415
return false;
1516
}
1617

17-
int [] counter = new int[26];
18+
int[] counter = new int[26];
1819
for (int i = 0; i < s.length(); i++) {
1920
counter[s.charAt(i) - 'a']++;
2021
counter[t.charAt(i) - 'a']--;
@@ -30,11 +31,10 @@ public boolean isAnagram(String s, String t) {
3031

3132

3233
@Test
33-
public void testCase(){
34+
public void testCase() {
3435

35-
System.out.println(isAnagram("anagram","nagaram"));
36+
System.out.println(isAnagram("anagram", "nagaram"));
3637

3738
}
3839

39-
4040
}

src/main/java/com/chen/algorithm/study/test92/Solution2.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.chen.algorithm.study.test92;
22

33

4+
import org.junit.Test;
5+
46
/**
57
* @author Chen WeiJie
68
* @date 2020-05-27 17:39:32
9+
* @Description: zhunn 反转链表2
710
**/
811
public class Solution2 {
912

@@ -43,4 +46,34 @@ public ListNode reverseBetween(ListNode head, int m, int n) {
4346

4447
return dummy.next;
4548
}
49+
50+
@Test
51+
public void test() {
52+
ListNode l1_1 = new ListNode(7);
53+
ListNode l1_2 = new ListNode(9);
54+
ListNode l1_3 = new ListNode(2);
55+
ListNode l1_4 = new ListNode(10);
56+
ListNode l1_5 = new ListNode(1);
57+
ListNode l1_6 = new ListNode(8);
58+
ListNode l1_7 = new ListNode(6);
59+
60+
l1_1.next = l1_2;
61+
l1_2.next = l1_3;
62+
l1_3.next = l1_4;
63+
l1_4.next = l1_5;
64+
l1_5.next = l1_6;
65+
l1_6.next = l1_7;
66+
67+
ListNode result = reverseBetween(l1_1,3,6);
68+
69+
System.out.println(result.val);
70+
System.out.println(result.next.val);
71+
System.out.println(result.next.next.val);
72+
System.out.println(result.next.next.next.val);
73+
System.out.println(result.next.next.next.next.val);
74+
System.out.println(result.next.next.next.next.next.val);
75+
System.out.println(result.next.next.next.next.next.next.val);
76+
}
77+
78+
4679
}

0 commit comments

Comments
 (0)