Skip to content

Commit 73ee8a5

Browse files
authored
Create Delete the Middle Node of a Linked List.java
1 parent 5632d7d commit 73ee8a5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode deleteMiddle(ListNode head) {
13+
if (head.next == null) {
14+
return null;
15+
}
16+
ListNode slow = head;
17+
ListNode fast = head.next.next;
18+
while (fast != null && fast.next != null) {
19+
slow = slow.next;
20+
fast = fast.next.next;
21+
}
22+
slow.next = slow.next.next;
23+
return head;
24+
}
25+
}

0 commit comments

Comments
 (0)