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

 
Example

Given 1->2->3->4, and node 3. return 1->2->4

 

LeetCode上的原题,请参见我之前的博客Delete Node in a Linked List

 

class Solution {
public:
    /**
     * @param node: a node in the list should be deleted
     * @return: nothing
     */
    void deleteNode(ListNode *node) {
        node->val = node->next->val;
        ListNode *tmp = node->next;
        node->next = tmp->next;
        delete tmp;
    }
};

 

相关文章: