Interview Question

Principal Staff Engineer Interview

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

Answer

Interview Answer

1 Answer

0

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;
        }
    }

Harsha on Apr 4, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.