-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode876.java
More file actions
29 lines (25 loc) · 845 Bytes
/
LeetCode876.java
File metadata and controls
29 lines (25 loc) · 845 Bytes
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
import util.ListNode;
public class LeetCode876 {
public static void main(String[] args) {
// 输入:head = [1,2,3,4,5]
// 输出:[3,4,5]
System.out.println(new Solution876().middleNode(ListNode.buildLinkedList(new Integer[] { 1, 2, 3, 4, 5 })));
// 输入:head = [1,2,3,4,5,6]
// 输出:[4,5,6]
System.out.println(new Solution876().middleNode(ListNode.buildLinkedList(new Integer[] { 1, 2, 3, 4, 5, 6 })));
}
}
class Solution876 {
public ListNode middleNode(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
if (fast.next != null) {
slow = slow.next;
}
return slow;
}
}