-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN2095_Delete_Middle_Node.java
More file actions
38 lines (33 loc) · 1.27 KB
/
Copy pathN2095_Delete_Middle_Node.java
File metadata and controls
38 lines (33 loc) · 1.27 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
public class N2095_Delete_Middle_Node {
public static ListNode deleteMiddle(ListNode head) {
if (head.next == null) return null;
if (head.next.next == null) {
head.next = null;
return head;
}
ListNode temp1 = head, temp2 = head;
while (temp2.next != null && temp2.next.next != null&& temp2.next.next.next != null) {
temp1 = temp1.next;
temp2 = temp2.next.next;
}
temp1.next = temp1.next.next;
return head;
}
public static void main(String[] args) {
ListNode l7 = new ListNode(7);
ListNode l6 = new ListNode(6,l7);
ListNode l5 = new ListNode(5, l6);
ListNode l4 = new ListNode(4, l5);
ListNode l3 = new ListNode(3, l4);
ListNode l2 = new ListNode(2, l3);
ListNode l1 = new ListNode(1, l2);
deleteMiddle(l1);
System.out.print("[");
for (ListNode i = l1; i != null; i = i.next) {
System.out.print(i.val + ",");
}
System.out.println("]");
}
}
//Runtime: 5 ms, faster than 60.54% of Java online submissions for Delete the Middle Node of a Linked List.
//Memory Usage: 209 MB, less than 57.38% of Java online submissions for Delete the Middle Node of a Linked List.