美文网首页
LeetCode: Delete Node in a Linke

LeetCode: Delete Node in a Linke

作者: 静之先生 | 来源:发表于2015-07-20 19:02 被阅读18次

    思路:把要删除的节点的下一个节点的值放到被删除的节点处,删除下一个节点.

    /**
     * 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) {
            if(node->next == NULL || node == NULL) return;
            node->val = node->next->val;
            node->next = node->next->next;
        }
    };
    

    相关文章

      网友评论

          本文标题:LeetCode: Delete Node in a Linke

          本文链接:https://www.haomeiwen.com/subject/hcppqttx.html