![]() |
VOOZH | about |
Given a singly linked list, delete a node at the kth position without using the loop.
Examples:
Input : list = 9->8->3->5->2->1 k = 4 Output : 9->8->3->2->1 Input : list = 0->0->1->6->2->3 k = 3 Output : 0->0->6->2->3
We recursively reduce the value of k. When k reaches 1, we delete the current node and return the next current node as a new node. When the function returns, we link the returned node to the next previous node.
Implementation:
Modified Linked List: 12 15 11 5 6 2 3
Time Complexity: O(n)
Auxiliary Space: O(n)