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

Commit efb59bb

Browse files
committed
增加链表相关解答
1 parent f1bdebe commit efb59bb

13 files changed

+426
-270
lines changed

.idea/misc.xml

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+189-119
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LinkedList/No141.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
package Algorithm.leetcode.LinkedList;
1+
package LinkedList;
22

33
public class No141 {
4-
public boolean hasCycle(ListNode head) {
5-
if (head == null || head.next == null) {
6-
return false;
7-
}
8-
ListNode fast = head;
9-
ListNode slow = head;
10-
if (fast.next.next == null) {
11-
return false;
12-
}
13-
while (fast != null && fast.next != null) {
14-
fast = fast.next.next;
15-
slow = slow.next;
16-
if (fast == slow) {
17-
return true;
18-
}
19-
}
20-
return false;
21-
}
4+
public boolean hasCycle(ListNode head) {
5+
if (head == null || head.next == null) {
6+
return false;
7+
}
8+
ListNode fast = head;
9+
ListNode slow = head;
10+
if (fast.next.next == null) {
11+
return false;
12+
}
13+
while (fast != null && fast.next != null) {
14+
fast = fast.next.next;
15+
slow = slow.next;
16+
if (fast == slow) {
17+
return true;
18+
}
19+
}
20+
return false;
21+
}
2222
}

LinkedList/No147.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package LinkedList;
2+
3+
/**
4+
* @author tujietg
5+
* @date 6/11/20 3:43 PM
6+
*/
7+
public class No147 {
8+
9+
public ListNode insertionSortList(ListNode head) {
10+
11+
}
12+
13+
}

LinkedList/No160.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
package Algorithm.leetcode.LinkedList;
1+
package LinkedList;
22

33
public class No160 {
4-
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
5-
if (headA == null || headB == null) {
6-
return null;
7-
}
8-
ListNode a = headA;
9-
ListNode b = headB;
10-
while (a != b) {
11-
a = a == null ? headB : a.next;
12-
b = b == null ? headA : b.next;
13-
}
14-
return a;
15-
}
4+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
5+
if (headA == null || headB == null) {
6+
return null;
7+
}
8+
ListNode a = headA;
9+
ListNode b = headB;
10+
while (a != b) {
11+
a = a == null ? headB : a.next;
12+
b = b == null ? headA : b.next;
13+
}
14+
return a;
15+
}
1616
}

LinkedList/No203.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package Algorithm.leetcode.LinkedList;
1+
package LinkedList;
22

33
public class No203 {
4-
public ListNode removeElements(ListNode head, int val) {
5-
ListNode prehead = new ListNode(-1);
6-
prehead.next = head;
7-
ListNode cur = head, pre = prehead;
8-
while (cur != null) {
9-
if (cur.val == val) {
10-
pre.next = cur.next;
11-
} else {
12-
pre = pre.next;
13-
}
14-
cur = cur.next;
15-
}
16-
return prehead.next;
17-
}
4+
public ListNode removeElements(ListNode head, int val) {
5+
ListNode prehead = new ListNode(-1);
6+
prehead.next = head;
7+
ListNode cur = head, pre = prehead;
8+
while (cur != null) {
9+
if (cur.val == val) {
10+
pre.next = cur.next;
11+
} else {
12+
pre = pre.next;
13+
}
14+
cur = cur.next;
15+
}
16+
return prehead.next;
17+
}
1818
}

LinkedList/No206.java

+45-45
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
package Algorithm.leetcode.LinkedList;
1+
package LinkedList;
22

33
public class No206 {
4-
// 迭代
5-
public ListNode reverseList02(ListNode head) {
6-
if (head == null || head.next == null) {
7-
return head;
8-
}
9-
ListNode tem = null;
10-
ListNode newList = null;
11-
while (head != null) {
12-
tem = head.next;
13-
head.next = newList;
14-
newList = head;
15-
head = tem;
16-
}
17-
return newList;
18-
}
4+
// 迭代
5+
public ListNode reverseList02(ListNode head) {
6+
if (head == null || head.next == null) {
7+
return head;
8+
}
9+
ListNode tem = null;
10+
ListNode newList = null;
11+
while (head != null) {
12+
tem = head.next;
13+
head.next = newList;
14+
newList = head;
15+
head = tem;
16+
}
17+
return newList;
18+
}
1919

20-
// 递归
21-
public ListNode reverseList01(ListNode head) {
22-
if (head == null || head.next == null) {
23-
return head;
24-
}
25-
ListNode newList = reverseList01(head.next);
26-
head.next.next = head;
27-
head.next = null;
28-
return newList;
29-
}
20+
// 递归
21+
public ListNode reverseList01(ListNode head) {
22+
if (head == null || head.next == null) {
23+
return head;
24+
}
25+
ListNode newList = reverseList01(head.next);
26+
head.next.next = head;
27+
head.next = null;
28+
return newList;
29+
}
3030

31-
// 三指针解决、
32-
public ListNode reverseList(ListNode head) {
33-
if (head == null) {
34-
return null;
35-
}
36-
ListNode current = head;
37-
ListNode pre = null;
38-
ListNode newHead = null;
39-
while (current != null) {
40-
ListNode next = current.next;
41-
if (next == null) {
42-
newHead = current;
43-
}
44-
current.next = pre;
45-
pre = current;
46-
current = next;
47-
}
48-
return newHead;
49-
}
31+
// 三指针解决、
32+
public ListNode reverseList(ListNode head) {
33+
if (head == null) {
34+
return null;
35+
}
36+
ListNode current = head;
37+
ListNode pre = null;
38+
ListNode newHead = null;
39+
while (current != null) {
40+
ListNode next = current.next;
41+
if (next == null) {
42+
newHead = current;
43+
}
44+
current.next = pre;
45+
pre = current;
46+
current = next;
47+
}
48+
return newHead;
49+
}
5050

5151
}

LinkedList/No234.java

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
package Algorithm.leetcode.LinkedList;
1+
package LinkedList;
22

33
//回文链表1->2->2->1
44
public class No234 {
5-
boolean a = true;
6-
ListNode hd;
5+
boolean a = true;
6+
ListNode hd;
77

8-
public boolean isPalindrome(ListNode head) {
9-
if (head == null || head.next == null) {
10-
return true;
11-
}
12-
hd = head;
13-
ListNode end = head;
14-
demo(end);
15-
return a;
16-
}
8+
public boolean isPalindrome(ListNode head) {
9+
if (head == null || head.next == null) {
10+
return true;
11+
}
12+
hd = head;
13+
ListNode end = head;
14+
demo(end);
15+
return a;
16+
}
1717

18-
public void demo(ListNode end) {
19-
if (end.next == null) {
20-
return;
21-
}
22-
end = end.next;
23-
demo(end);
24-
if (hd.val == end.val) {
25-
hd = hd.next;
26-
a = a & true;
27-
} else {
28-
a = a & false;
29-
}
30-
}
18+
public void demo(ListNode end) {
19+
if (end.next == null) {
20+
return;
21+
}
22+
end = end.next;
23+
demo(end);
24+
if (hd.val == end.val) {
25+
hd = hd.next;
26+
a = a & true;
27+
} else {
28+
a = a & false;
29+
}
30+
}
3131

3232
}

LinkedList/No237.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package Algorithm.leetcode.LinkedList;
1+
package LinkedList;
22

33
public class No237 {
4-
public void deleteNode(ListNode node) {
5-
node.val = node.next.val;
6-
node.next = node.next.next;
7-
}
4+
public void deleteNode(ListNode node) {
5+
node.val = node.next.val;
6+
node.next = node.next.next;
7+
}
88
}

LinkedList/No83.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
package Algorithm.leetcode.LinkedList;
1+
package LinkedList;
22

33
/**
44
* Definition for singly-linked list. public class ListNode { int val; ListNode
55
* next; ListNode(int x) { val = x; } }
66
*/
77
class Solution {
8-
public ListNode deleteDuplicates(ListNode head) {
9-
ListNode list = head;
10-
while (list != null) {
11-
if (list.next == null) {
12-
break;
13-
}
14-
if (list.val == list.next.val) {
15-
list.next = list.next.next;
16-
} else {
17-
list = list.next;
18-
}
19-
}
20-
return head;
21-
}
8+
public ListNode deleteDuplicates(ListNode head) {
9+
ListNode list = head;
10+
while (list != null) {
11+
if (list.next == null) {
12+
break;
13+
}
14+
if (list.val == list.next.val) {
15+
list.next = list.next.next;
16+
} else {
17+
list = list.next;
18+
}
19+
}
20+
return head;
21+
}
2222
}

LinkedList/Nom0201.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package LinkedList;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author tujietg
8+
* @date 6/11/20 2:54 PM
9+
*/
10+
public class Nom0201 {
11+
12+
public ListNode removeDuplicateNodes(ListNode head) {
13+
14+
Map<Integer, Integer> map = new HashMap<>();
15+
16+
ListNode preNode = head;
17+
ListNode lastNode = null;
18+
19+
while (preNode != null) {
20+
if (!map.containsKey(preNode.val)) {
21+
map.put(preNode.val,1);
22+
lastNode = preNode;
23+
preNode = preNode.next;
24+
}else {
25+
preNode = preNode.next;
26+
lastNode.next = preNode;
27+
}
28+
}
29+
return head;
30+
}
31+
}

0 commit comments

Comments
 (0)