Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Delete Node in a Linked List leetcode

class Solution {
public:
    void deleteNode(ListNode* node) {
        //Make the given node to the next node and delete one of them since they now same
        //Say linked list is 4 -> 5 -> 1 -> 9, node = 5
        
        node -> val = node -> next -> val;
        
        //Now it'll be 4 -> 1 -> 1 -> 9, now our node is the first 1
        
        node -> next = node -> next -> next;
        
        //Now it'll be 4 -> 1 -> 9 (the second 1's next link is cut off)
        //That's the whole answer we don't have to return anything
    }
};
 
PREVIOUS NEXT
Tagged: #Delete #Node #Linked #List #leetcode
ADD COMMENT
Topic
Name
5+1 =