
Problem
Solution
Submissions
Remove Nth Node
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a C program to remove the nth node from the end of a linked list and return its head. Given a linked list, remove the n-th node from the end of the list and return the modified list's head.
Example 1
- Input: head = [1, 2, 3, 4, 5], n = 2
- Output: [1, 2, 3, 5]
- Explanation: The linked list is 1->2->3->4->5. We need to remove the 2nd node from the end, which is 4. After removal, the linked list becomes 1->2->3->5.
Example 2
- Input: head = [1], n = 1
- Output: []
- Explanation: The linked list only has one node: 1. We need to remove the 1st node from the end, which is the only node. After removal, the linked list is empty.
Constraints
- The number of nodes in the list is sz.
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= sz
- Time Complexity: O(L) where L is the length of the list
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use the two-pointer technique with a fast and slow pointer
- Move the fast pointer n steps ahead first
- Then move both pointers together until the fast pointer reaches the end
- At this point, the slow pointer will be just before the node to be removed
- Update the next pointer of the slow pointer to skip the node to be removed