905. Deletion of the Kth element of Linked List

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.

Example 1:

Input: linkedList = [3, 4, 5], k = 2

Output: linkedList = [3, 5]

Explanation:

The 2nd node with value 4 was removed.

Example 2:

Input: linkedList = [1, 2, 3], k = 1

Output: [2, 3]

Explanation:

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

Now Your Turn!

Pick the correct output for the given input

Input: linkedList = [7, 7, 7], k = 3

Still unsure what the problem is asking ?

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

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/*
Definition of singly linked list:
class ListNode{
public:
int data;
ListNode *next;
ListNode() : data(0), next(nullptr) {}
ListNode(int x) : data(x), next(nullptr) {}
ListNode(int x, ListNode *next) : data(x), next(next) {}
};
*/
 
class Solution {
public:
ListNode* deleteKthNode(ListNode* &head, int k) {
// Your code goes here
}
};
 
Test Case

Input:

K
Linked List