Remove the middle node from a singly linked list and return the updated head.
Delete Middle Node of a Linked List
gfgGiven the head of a singly linked list, remove the middle node and return the head of the modified list.
For a list with an even number of nodes, delete the second of the two middle nodes.
You should perform the deletion by changing links in the list, not by rebuilding the entire list.
Input Format
- A singly linked list is given by its head node.
- Each node contains an integer value and a pointer to the next node.
- The list contains at least one node.
Output Format
- Return the head of the linked list after removing its middle node.
Constraints
- Node values may be any valid integers.
- The list is singly linked.
- When is even, remove the second middle node.
Example 1
Input
head = [1,3,4,7,1,2,6]
Output
[1,3,4,1,2,6]
Explanation
The list has 7 nodes, so the middle node is 7. Removing it leaves the remaining nodes linked in order.
Example 2
Input
head = [1,2,3,4]
Output
[1,2,4]
Explanation
The list has 4 nodes, so the second middle node is 3. Removing it produces [1,2,4].
Show 1 more example
Example 3
Input
head = [2]
Output
[]
Explanation
A single-node list has its only node as the middle, so deleting it leaves an empty list.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.