-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode2095.java
More file actions
36 lines (31 loc) · 1.14 KB
/
LeetCode2095.java
File metadata and controls
36 lines (31 loc) · 1.14 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
import util.ListNode;
public class LeetCode2095 {
public static void main(String[] args) {
// 输入:head = [1,3,4,7,1,2,6]
// 输出:[1,3,4,1,2,6]
System.out.println(
new Solution2095().deleteMiddle(ListNode.buildLinkedList(new Integer[] { 1, 3, 4, 7, 1, 2, 6 })));
// 输入:head = [1,2,3,4]
// 输出:[1,2,4]
System.out.println(
new Solution2095().deleteMiddle(ListNode.buildLinkedList(new Integer[] { 1, 2, 3, 4 })));
// 输入:head = [2,1]
// 输出:[2]
System.out.println(
new Solution2095().deleteMiddle(ListNode.buildLinkedList(new Integer[] { 2, 1 })));
}
}
class Solution2095 {
public ListNode deleteMiddle(ListNode head) {
ListNode dummyNode = new ListNode();
dummyNode.next = head;
ListNode slow = dummyNode;
ListNode fast = dummyNode;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
slow.next = slow.next == null ? null : slow.next.next;
return dummyNode.next;
}
}