Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 578 Bytes

DeleteMiddleNode.md

File metadata and controls

25 lines (18 loc) · 578 Bytes

Delete Middle Node

Implement an algorithm to delete a node in the middle of a singly linked list given only access to that node.

SOL

If we are only given the access to that node, we will have no way to access the previous node. Therefore, copying the value and set it to the next node is the only way to go.

public void deleteNode(LinkedListNode n) {
    LinkedListNode temp = n.next;
    if (temp == null) {
        n = null; // if n is the last node, just set it to be null
    } else {
        n.data = temp.data;
        n.next = temp.next;  
    }
}