Skip to content

Commit dafa0c1

Browse files
Create DeleteMiddleLinkedList.java
1 parent cccef84 commit dafa0c1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
2095. Delete the Middle Node of a Linked List
3+
Solved
4+
Medium
5+
Topics
6+
Companies
7+
Hint
8+
You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list.
9+
10+
The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.
11+
**/
12+
class Solution {
13+
public ListNode deleteMiddle(ListNode head) {
14+
if(head == null || head.next == null) return null;
15+
16+
ListNode slow = head;
17+
ListNode fast = head;
18+
ListNode prev = null;
19+
20+
while(fast != null && fast.next != null) {
21+
prev = slow;
22+
slow = slow.next;
23+
fast = fast.next.next;
24+
}
25+
26+
prev.next = slow.next;
27+
return head;
28+
}
29+
}

0 commit comments

Comments
 (0)