单链表删除节点的方法

public class ListNode {

    int val;

    ListNode next;

    ListNode(int x) { val = x; }

}

删除一个单链表里的某个指定的节点:

1.修改指针指向的对象


public static void deleteNodeV2(ListNode head, ListNode node) {

    if(head == null || node == null) {

        return;

    }

    while (head != null) {

        if(head.next.val == node.val) {

            head.next = head.next.next;

            return;

        }

        head = head.next;

    }

}

2.指针指向的对象不变,节点的值覆盖,需要被删除node不是尾节点

public static void deleteNode(ListNode node) {

    if(node == null || node.next == null) {

        return;

    }

    node.val = node.next.val;

    node.next = node.next.next;

    return;

}

---------------------

作者:翁正存

来源:CSDN

原文:https://blog.csdn.net/Wengzhengcun/article/details/87971694

版权声明:本文为博主原创文章,转载请附上博文链接!

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容