File tree 13 files changed +426
-270
lines changed
13 files changed +426
-270
lines changed Original file line number Diff line number Diff line change 1
- package Algorithm . leetcode . LinkedList ;
1
+ package LinkedList ;
2
2
3
3
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
+ }
22
22
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- package Algorithm . leetcode . LinkedList ;
1
+ package LinkedList ;
2
2
3
3
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
+ }
16
16
}
Original file line number Diff line number Diff line change 1
- package Algorithm . leetcode . LinkedList ;
1
+ package LinkedList ;
2
2
3
3
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
+ }
18
18
}
Original file line number Diff line number Diff line change 1
- package Algorithm . leetcode . LinkedList ;
1
+ package LinkedList ;
2
2
3
3
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
+ }
19
19
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
+ }
30
30
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
+ }
50
50
51
51
}
Original file line number Diff line number Diff line change 1
- package Algorithm . leetcode . LinkedList ;
1
+ package LinkedList ;
2
2
3
3
//回文链表1->2->2->1
4
4
public class No234 {
5
- boolean a = true ;
6
- ListNode hd ;
5
+ boolean a = true ;
6
+ ListNode hd ;
7
7
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
+ }
17
17
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
+ }
31
31
32
32
}
Original file line number Diff line number Diff line change 1
- package Algorithm . leetcode . LinkedList ;
1
+ package LinkedList ;
2
2
3
3
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
+ }
8
8
}
Original file line number Diff line number Diff line change 1
- package Algorithm . leetcode . LinkedList ;
1
+ package LinkedList ;
2
2
3
3
/**
4
4
* Definition for singly-linked list. public class ListNode { int val; ListNode
5
5
* next; ListNode(int x) { val = x; } }
6
6
*/
7
7
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
+ }
22
22
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments