Unlink the Later Middle Node. Delete the node at zero-based position floor(n/2) from the linked list and return the modified ListNode* head. For even lengths this is the later of the two middle nodes. A one-node list becomes empty.
### Problem
Given the head of a singly linked list with `n` nodes, remove the node at zero-based position `floor(n / 2)` and return the head of the remaining list. For even `n`, this means the later of the two middle nodes. A one-node list becomes empty. Keep the other nodes in their original order.
```cpp
class Solution {
public:
ListNode* deleteMiddle(ListNode* head);
};
```
### Examples
**Example 1:**
```text
List values: [8,6,4,2,1]
Output list values: [8,6,2,1]
Explanation: The removed position is floor(5/2)=2, holding 4.
```
**Example 2:**
```text
List values: [7]
Output: nullptr
Explanation: Position zero is the middle and is removed.
```
### Constraints
- The list has 1 to `10^5` nodes.
- Node values lie in `[1,10^5]`.
### Clarifying Questions
Delete the node from the reachable chain by changing links; do not replace values to imitate deletion. Memory ownership/deallocation is not specified by the source.
```hint Inspect the structure
Where is a one-step pointer when a two-step pointer reaches the end?
```
### Reasoning checkpoints
- Remove exactly the specified middle position for both odd and even list lengths.
- Preserve the remaining nodes' identities, values and relative order.
- Handle a singleton without dereferencing a missing predecessor or successor.
### Optional extensions
How would you adapt the solution if the earlier middle were requested for an even-length list?
Overview: Unlink the Later Middle Node. Delete the node at zero-based position floor(n/2) from the linked list and return the modified ListNode* head. For even lengths this is the later of the two middle nodes. A one-node list becomes empty.
Given the head of a singly linked list with n nodes, remove the node at zero-based position floor(n / 2) and return the head of the remaining list. For even n, this means the later of the two middle nodes. A one-node list becomes empty. Keep the other nodes in their original order.
class Solution {
public:
ListNode* deleteMiddle(ListNode* head);
};
Examples
Example 1:
List values: [8,6,4,2,1]
Output list values: [8,6,2,1]
Explanation: The removed position is floor(5/2)=2, holding 4.
Example 2:
List values: [7]
Output: nullptr
Explanation: Position zero is the middle and is removed.
Constraints
The list has 1 to
10^5
nodes.
Node values lie in
[1,10^5]
.
Clarifying Questions Guidance
Delete the node from the reachable chain by changing links; do not replace values to imitate deletion. Memory ownership/deallocation is not specified by the source.
Reasoning checkpoints
Remove exactly the specified middle position for both odd and even list lengths.
Preserve the remaining nodes' identities, values and relative order.
Handle a singleton without dereferencing a missing predecessor or successor.
Optional extensions
How would you adapt the solution if the earlier middle were requested for an even-length list?