Given a doubly-linked list, write a method to delete the node at a given position (starting from 1 as the head position) and return the modified list's head. Do nothing if the input position is out of range.
1<=>2<=>3<=>4, pos=6 ==> 1<=>2<=>3<=>4
1<=>2<=>3<=>4, pos=3 ==> 1<=>2<=>4
1. Create two temporary references to keep track of the current and previous nodes in the traversal. Reach the required position in the list.
2. Point previous
's next
to currrent
's next
.
3. Point current
's next
's previous
to previous
. i.e. currentNode.next.prev = prevNode;
4. Point current
's next
and previous
to null
.
5. Return the head
.
public DoublyLinkedNode deleteAtPos(DoublyLinkedNode head, int pos) { }
C
Java
Python