81. Remove Nth node from the back of the LL

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.

Example 1:

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.

Example 2:

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.

Now Your Turn!

Pick the correct output for the given input

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

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/*
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) {
 
}
};
Test Case

Input:

N
Nums