剑指OFFER----面试题18. 删除链表的节点

链接:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/submissions/

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

 class Solution {
public:
    ListNode* deleteNode(ListNode* head, int val) {
        if (head->val == val) return head->next;
        ListNode* ret = head;
        while (ret->next->val != val) ret = ret->next;
        ret->next = ret->next->next;
        return head;
    }
};

猜你喜欢

转载自www.cnblogs.com/clown9804/p/12340429.html