Motorola Mobility interview question

Write code for inserting a node in a doubly linked list.

Interview Answer

Anonymous

5 Apr 2014

public void insert(int index, int d){ Node current = head; while(index>0){ current = current.next; index--; } Node temp = current.next; Node inserted = new Node(temp, current, d); current.next = inserted; if(temp!=null){ temp.previous = inserted; } }