[LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

 

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;
    }
};

 

posted @ 2016-11-08 10:54  Grandyang  阅读(494)  评论(0编辑  收藏  举报
Fork me on GitHub