Skip to content

Latest commit

 

History

History
52 lines (37 loc) · 1.15 KB

剑指offer-18 Delete element in linkList.md

File metadata and controls

52 lines (37 loc) · 1.15 KB

给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。

返回删除后的链表的头节点。

Example:

示例 1:

输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

示例 2:

输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

Solution

/**
 * 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* rsp = head;
        while (head->next->val != val)
            head = head->next;
        head->next = head->next->next;

        return rsp;
    }
};

执行用时:8 ms, 在所有 C++ 提交中击败了96.60%的用户

内存消耗:9.1 MB, 在所有 C++ 提交中击败了37.12%的用户