Deletion of the Kth element of LL

Linked-List Fundamentals (Single LL) Easy

Given the head of a singly linked list and an integer k, delete the kth node of the linked list and return the head of the modified list.

Examples:

Input: head -> 3 -> 4 -> 5, k = 2

Output: head -> 3 -> 5

Explanation: The 2nd node with value 4 was removed.

Input: head -> 1 -> 2 -> 3, k = 1

Output: head -> 2 -> 3

Explanation: The 1st Node was removed, note that the value of the head has changed.

Input: head -> 7 -> 7 -> 7, k = 3

Constraints

  • n == number of nodes in the Linked list
  • 1 <= n <= 1000
  • 0 <= ListNode.val <= 100
  • 1 <= k <= n

Hints

  • If the list is empty (head == None), return None. If k == 1, remove the head node and return head.next. Traverse the list to find the k-th node.
  • Update the pointers, If the node has a previous node (prev), update prev.next = node.next. If the node has a next node (next), update next.prev = node.prev. If k is beyond the length of the list, return the original head.

Company Tags

OYO Rooms Square Teladoc Health Activision Blizzard Walmart Morgan Stanley Bloomberg Red Hat Rakuten Micron Technology Philips Healthcare Johnson & Johnson McKinsey & Company Uber Reddit Riot Games JPMorgan Chase Electronic Arts Cloudflare eBay Salesforce Siemens Healthineers KPMG Cerner Epic Systems TCS Cognizant Accenture Infosys Capgemini Wipro