关于leetcode:每日一练10删除链表的节点

4次阅读

共计 1613 个字符,预计需要花费 5 分钟才能阅读完成。


title: 每日一练(10):删除链表的节点

categories:[剑指 offer]

tags:[每日一练]

date: 2022/01/23


每日一练(10):删除链表的节点

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

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

留神:此题比照原题有改变

示例 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.

阐明:

题目保障链表中节点的值互不雷同

若应用 C 或 C++ 语言,你不须要 free 或 delete 被删除的节点

起源:力扣(LeetCode)

链接:https://leetcode-cn.com/probl…

办法一:惯例解法(循环遍历)

如果头部的值是 val 间接返回头的 next 即可,不是遍历头部,找到 val 后间接跳过该节点,并返回 head 即可

ListNode* deleteNode(ListNode* head, int val) {if (head->val == val) { // 头部的值是 val 间接返回头的 next
        return head->next;
    }
    ListNode *node = head;
    while (node->next != nullptr) {// 遍历
        if (node->next->val == val) {
            node->next = node->next->next;
            return head;
        }
        node = node->next;
    }
    return head;
}

办法二:递归

递归的形式就比拟奇妙,能够了解从尾部到头判断,空则返回空,非空判断是否等于 val,等于返回其 next,不等于则返回自身

// 递归 1
ListNode* deleteNode(ListNode* head, int val) {if (head == nullptr) {return nullptr;}
    
    head->next = deleteNode(head->next, val);
    if (head->val == val) {return head->next;}
    return head;
}
// 递归 2
ListNode* deleteNode(ListNode* head, int val) {if (head == nullptr) {return nullptr;}
    
    head->next = deleteNode(head->next, val);
    // 三目运算符
    return head->val == val ? head->next : head;
}

办法三:双指针

设置两个指针别离指向头节点,pre(要删除节点的前一节点)和 cur (以后节点);

遍历整个链表,查找节点值为 val 的节点,找到了就删除该节点,否则持续查找。

1. 找到,将以后节点的前驱节点(pre 节点或者说是之前最近一个值不等于 val 的节点)连贯到以后节点(cur 节点)的下一个节点(pre->next = cur->next)。

2. 没找到,持续遍历(cur = cur->next),更新最近一个值不等于 val 的节点(pre = cur)。

ListNode* deleteNode(ListNode* head, int val) {if(head == NULL) {  //  特判
        return NULL;
    }
    if(head->val == val) {  //  头节点为要删除的节点
        return head->next;
    }

    ListNode* cur = head;  //  以后节点
    ListNode* pre = head;  //  保留删除节点的前一节点
    while (cur != NULL && cur->val != val) {
        pre = cur;
        cur = cur->next;
    }
    if (cur != NULL) {pre->next = cur->next;}

    return head;
}
正文完
 0