Amazon Interview Question: Write a function to delete al... | Glassdoor

Interview Question

Software Development Engineer Intern Interview Seattle, WA

Write a function to delete all nodes from a linked list

  that match an input value. Provide test cases.
Answer

Interview Answer

1 Answer

0

public void deleteNodes(int key) {
    Node node = headNode;
    while(node.next != null) {
        if(node.next.data == key)
             node.next = node.next.next;
         else
             node = node.next;
    }
    if(headNode.data == key) {
         headNode = headNode.next;
}

codewarrior on Mar 2, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.