0
votes

QUESTION

Write a function that receives as input the head node of a linked list an integer k.

Your function should remove the kth node from the end of the linked list and return the head node of the updated list.

EXAMPLE LINKED LIST

(20) -> (19) -> (18) -> (17) -> (16) -> (15) -> (14) -> (13) -> (12) -> (11) -> null

Head node would refer to the node (20). Let k = 4, so it should remove the 4th node of the list, node (14).

The new list should have node (14) removed from it.

CODE

const linkedList = node => {

let head = node;


}

I'm not sure how to go about this. I define a function with node as a parameter and I need to be able to differentiate each node and be able to traverse through and remove the one in question.

Any starter tips or pointers?

1

1 Answers

0
votes

One of the ways:

0) Always keep a temporary copy of the head of the list

1) Count the total number of nodes in the linked list, let's call it n.

2) Let's say you have to delete the xth node from the end, so n-x+1th node from the start.

3) Go till the n-xth node and delete the next node(refer to standard way on how to delete a node in a linked list).