888. Delete Kth Element of Doubly Linked List

Given the head of a doubly linked list and an integer k, remove the node at the kth position of the linked list and return the head of the modified list.

Example 1:

Input: head = [2, 5, 7, 9], k = 2

Output: head = [2, 7, 9]

Explanation:

  • The node with value 5 was removed.

Example 2:

Input: head = [2, 5, 7], k = 1

Output: head = [5, 7]

Explanation:

The node with value 2 was removed, note that the head was modified.

Now Your Turn!

Pick the correct output for the given input

Input: head = [2, 5, 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 <= 100
  • 0 <= ListNode.val <= 100
  • 1 <= k <= n

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
class ListNode
{
* Definition for doubly-linked list.
* public:
* int data;
* ListNode *prev;
* ListNode *next;
* ListNode() : data(0), prev(nullptr), next(nullptr) {}
* ListNode(int x) : data(x), prev(nullptr), next(nullptr) {}
* ListNode(int x, ListNode *prev, ListNode *next) : data(x), prev(prev), next(next) {}
};
*/
 
class Solution {
public:
ListNode *deleteKthElement(ListNode *&head, int k) {
// Your code goes here
}
};
Test Case

Input:

K
Nums