Remove Nth node from the back of the LL

Asked atIntuit
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Given the head of a singly linked list and an integer n. Remove the nth node from the back of the linked List and return the head of the modified list. The value of n will always be less than or equal to the number of nodes in the linked list.

Input: linkedList = 1 -> 2 -> 3 -> 4 -> 5, n = 2 Output: 1 -> 2 -> 3 -> 5 Explanation: The 2nd node from the back was the node with value 4.

Input: linkedList = 5 -> 4 -> 3 -> 2 -> 1, n = 5 Output: 4 -> 3 -> 2 -> 1 Explanation: The 5th node from the back is the first node.

Input: linkedList = 9 -> 8 -> 7, n = 1

  • 1 <= number of nodes in the Linked List <= 105
  • 0 <= ListNode.val <= 104
  • 1 <= n <= number of nodes in the Linked List.

cpp

/*
Definition of singly linked list:
struct ListNode
{
    int val;
    ListNode *next;
    ListNode()
    {
        val = 0;
        next = NULL;
    }
    ListNode(int data1)
    {
        val = data1;
        next = NULL;
    }
    ListNode(int data1, ListNode *next1)
    {
        val = data1;
        next = next1;
    }
};
*/

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {

    }
};

java

/*Definition for Singly Linked List
class ListNode {
    int val;
    ListNode next;

    ListNode() {
        val = 0;
        next = null;
    }

    ListNode(int data1) {
        val = data1;
        next = null;
    }

    ListNode(int data1, ListNode next1) {
        val = data1;
        next = next1;
    }
}
 */

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {

    }
}

python

# Definition for Singly Linked List
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution:
    def removeNthFromEnd(self, head, n):

javascript

/*Definition for Singly Linked List
class ListNode {
    constructor(val = 0, next = null) {
        this.val = val;
        this.next = next;
    }
}
*/

class Solution {
    removeNthFromEnd(head, n) {

    }
}

csharp

/*
Definition for Singly Linked List
public class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int val=0, ListNode next=null) {
        this.val = val;
        this.next = next;
    }
}
*/

public class Solution {
    public ListNode RemoveNthFromEnd(ListNode head, int n) {

    }
}

go

func removeNthFromEnd(head *ListNode, n int) *ListNode {

}
Stuck? Show a way to structure it+
  1. 01Add a dummy before head
  2. 02Advance fast by n+1 nodes with input validation
  3. 03Move both pointers until fast is null
  4. 04Bypass slow.next and return dummy.next

Reference answer

Then expect these follow-ups

  • How would you do this with a length pass?

    Tests: follow-up reasoning

  • Why does the dummy simplify head deletion?

    Tests: correctness reasoning

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question