当前位置:首页 > 系统运维

我们一起学习删除链表的节点

本文转载自微信公众号「程序员千羽」,学习作者程序员千羽。删除转载本文请联系J程序员千羽公众号。链表

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

“GitHub : https://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_12_hammingWeight/Solution.java

删除链表的节点节点

“题目描述: 给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。学习返回删除后的删除链表的头节点。示例 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. 

解题思路:

本题删除值为 val 的链表节点分需为两步:定位节点、修改引用。节点

定位节点: 遍历链表,学习直到 head.val == val 时跳出,删除即可定位目标节点。链表 修改引用: 设节点 cur 的前驱节点为 pre ,后继节点为 cur.next ;则执行 pre.next = cur.next ,即可实现删除 cur 节点。

** 算法流程:

**特例处理: 当应删除头节点 head 时,直接返回 head.next 即可。 初始化: pre = head , cur = head.next 。 定位节点: 当 cur 为空 或 cur 节点值等于 val 时跳出。 保存当前节点索引,亿华云计算即 pre = cur 。 遍历下一节点,即 cur = cur.next 。 删除节点: 若 cur 指向某节点,则执行 pre.next = cur.next ;若 cur 指向 nullnull ,代表链表中不包含值为 val 的节点。 返回值: 返回链表头部节点 head 即可。

复杂度分析:

时间复杂度 O(N): N为链表长度,删除操作平均需循环 N/2 次,最差 N 次。 空间复杂度 O(1) : cur, pre 占用常数大小额外空间。 package com.nateshao.sword_offer.topic_15_deleteNode; /**  * @date Created by 邵桐杰 on 2021/11/21 16:22  * @微信公众号 程序员千羽  * @个人网站 www.nateshao.cn  * @博客 https://nateshao.gitee.io  * @GitHub https://github.com/nateshao  * @Gitee https://gitee.com/nateshao  * Description: 删除链表的节点  */ public class Solution {      public static void main(String[] args) {          ListNode listNode = new ListNode(3);         int val = 3;         ListNode node = deleteNode(listNode, val);         System.out.println("node = " + node);     }  // 推荐     public static ListNode deleteNode(ListNode head, int val) {          if (head.val == val) return head.next;         ListNode pre = head, cur = head.next;         while (cur != null && cur.val != val) {              pre = cur;             cur = cur.next;         }         if (cur != null) pre.next = cur.next;         return head;     }     public void deleteNode(ListNode head, ListNode deListNode) {          if (deListNode == null || head == null)             return;         if (head == deListNode) {              head = null;         } else {              // 若删除节点是末尾节点,往后移一个             if (deListNode.next == null) {                  ListNode pointListNode = head;                 while (pointListNode.next.next != null) {                      pointListNode = pointListNode.next;                 }                 pointListNode.next = null;             } else {                  deListNode.val = deListNode.next.val;                 deListNode.next = deListNode.next.next;             }         }     }     /**      * 单指针实现      *      * @param head      * @param val      * @return      */     public ListNode deleteNode2(ListNode head, int val) {          if (head == null) return null;         if (head.val == val) return head.next;         ListNode cur = head;         while (cur.next != null && cur.next.val != val) {              cur = cur.next;         }         if (cur.next != null) {              cur.next = cur.next.next;         }         return head;     }     /**      * 递归实现      *      * @param head      * @param val      * @return      */     public ListNode deleteNode3(ListNode head, int val) {          if (head == null) return null;         if (head.val == val) return head.next;         else head.next = deleteNode3(head.next, val);         return head;     }     public static class ListNode {          int val;         ListNode next;         ListNode(int x) {              val = x;         }     } } 

参考链接:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/solution/mian-shi-ti-18-shan-chu-lian-biao-de-jie-dian-sh-2

分享到:

滇ICP备2023006006号-16