Google

  www.google.com
Work in HR? Unlock Free Profile

Google Software Engineer Interview Question

"append an element to a double linked list."
Add Tags [?]
Answer

Part of a Software Engineer Interview Review - one of 3,048 Google Interview Reviews

Answers & Comments

0
of 0
votes

//In java
public void addElement(Item item) {
       if (item == null) {
           throw new NullPointerException("Null item passed");
       } else {

       Node oldFirst = first;
       Node newNode = new Node();
       newNode.item = item;
       newNode.next = oldFirst;
       newNode.prev = null;
       first = newNode;

       if (first.next == null) {
           last = first;
       } else {
           oldFirst.prev = first;
       }
       oldFirst = null; //release the memory
       }
   }

- paul odero on Apr 2, 2013

To comment on this question, Sign In with Facebook or Sign Up

Tags are like keywords that help categorize interview questions that have something in common.