Remove Nth node from the back of the LL

Linked-List Logic Building Medium

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.

Examples:

Input: head -> 1 -> 2 -> 3 -> 4 -> 5, n = 2

Output: head -> 1 -> 2 -> 3 -> 5

Explanation: The 2nd node from the back was the node with value 4.

Input: head -> 5 -> 4 -> 3 -> 2 -> 1, n = 5

Output: head -> 4 -> 3 -> 2 -> 1

Explanation: The 5th node from the back is the first node.

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

Constraints

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

Hints

  • A brute-force approach involves: Finding the length of the linked list (O(n)). Removing the (length - n + 1)th node from the start (O(n)).
  • Use two pointers: fast and slow. Move fast n steps ahead. Move slow and fast together until fast reaches the end. slow will now be just before the node to remove. Adjust pointers to delete the nth node from the end.

Company Tags

Zynga Byju's Electronic Arts Instacart Rockstar Games Walmart IBM Oracle Docker Ubisoft PayPal Airbnb Activision Blizzard Siemens Healthineers Micron Technology Qualcomm Dropbox KPMG Morgan Stanley Bloomberg HCL Technologies Teladoc Health Stripe Epic Systems Reddit Google Microsoft Amazon Meta Apple Netflix Adobe