-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListMid.java
More file actions
119 lines (107 loc) · 3.4 KB
/
LinkedListMid.java
File metadata and controls
119 lines (107 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package class01;
import java.util.ArrayList;
/**
* @author pacai
* @version 1.0
* 快慢指针
*/
public class LinkedListMid {
public static class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
}
}
Node<?> head;
//泛型方法
public<T> Node<T> midOrUpMidNode(Node<T> head) {
if (head == null || head.next == null || head.next.next == null) {
return head;
}
Node<T> fast = head.next.next;
Node<T> slow = head.next;
//链表有三个结点以上
while (fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
//验证方法
//奇数中点,偶数中点上
public<T> Node<T> right1(Node<T> head){
ArrayList<Node<T>> list = new ArrayList<>();
while(head != null){
list.add(head);
head = head.next;
}
return !list.isEmpty() ? list.get((list.size() - 1) / 2) : head;
}
//奇数中点,偶数中点下
public<T> Node<T> midOrDownMidNode(Node<T> head){
if(head == null || head.next == null){
return head;
}
Node<T> fast = head.next;
Node<T> slow = head.next;
while(fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
//验证方法
public<T> Node<T> right2(Node<T> head){
ArrayList<Node<T>> list = new ArrayList<>();
while(head != null){
list.add(head);
head = head.next;
}
return !list.isEmpty() ? list.get(list.size() / 2) : head;
}
//奇数返回中点前一个,偶数返回上中点前一个
public<T> Node<T> upMidOrDouUpMidNode(Node<T> head){
if(head == null || head.next == null || head.next.next == null){
return head;
}
Node<T> fast = head.next.next;
Node<T> slow = head;
if(fast.next != null && fast.next.next != null){ //5,7,9...才会往前走一步
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
//验证方法
public<T> Node<T> right3(Node<T> head){
ArrayList<Node<T>> list = new ArrayList<>();
while(head != null){
list.add(head);
head = head.next;
}
return (!list.isEmpty() && (list.size() >= 3)) ? list.get((list.size() - 1) / 2 - 1) : head;
}
//奇数返回中点前一个,偶数返回下中点前一个
public<T> Node<T> UpMidOrUpMidNode(Node<T> head){
if(head == null || head.next == null || head.next.next == null){
return head;
}
Node<T> fast = head.next;
Node<T> slow = head;
while(fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
//验证方法
public<T> Node<T> right4(Node<T> head){
ArrayList<Node<T>> list = new ArrayList<>();
while(head != null){
list.add(head);
head = head.next;
}
return (!list.isEmpty() && list.size() > 3) ? list.get(list.size() / 2 - 1) : head;
}
}