【Leetcode】237. Delete Node in a Linked List

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode * lastNode = node;
        while (node->next != NULL) {
            node->val = node->next->val;
            lastNode = node;
            node = node->next;
        }
        lastNode->next = NULL;
        delete node;      
    }
};

Your runtime beats 100.00 % of cpp submissions.

猜你喜欢

转载自www.cnblogs.com/AndrewGhost/p/9281146.html