面试题 13: 在O(1)时间删除链表节点

这是一篇关于如何在O(1)时间复杂度内删除单向链表节点的面试题解析。文章提供了问题描述,并提示读者可以在作者的代码库中找到解决方案代码,同时欢迎读者指出可能存在的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 题目

给定单向链表的头指针和一个节点指针,定义一个函数在O(1) 时间删除该节点.

代码请到我的代码库中下载 Point2Offer

二. 代码

package week_4;

/**难度系数:***
 * 剑指offer: 在O(1)时间删除链表节点
 * 方法: O(1)时间,不遍历前序结点,复制覆盖
 * 测试用例:功能测试(链表有多个结点/1个结点/为空/删除头结点/中间结点/尾结点)
 * @author dingding
 * Date:2017-7-5 17:10
 * Declaration: All Rights Reserved!
 */
public class No13 {

    public static void main(String[] args) {
        test1();
        test2();
        test3();
        test4();
        test5();
    }

    //O(n)时间,顺序遍历
    private static ListNode deleteNode_N(ListNode head,ListNode toBeDeleted){
        if (head == null || toBeDeleted == null) {
            System.out.println("链表为空.");
            return null;
        }
        //只有一个结点,将head置为空
        if (head.next == null) {
            return null;
        }

        ListNode pCur = head;  //当前指针
        while (pCur != null){
            if (head == toBeDeleted) {
                return head.next;
            }
            if (pCur.next == toBeDeleted) {
                //判断是否是最后一个结点
                if (pCur.next.next == null) {
                    pCur.next = null;
                    return head;
                }else {
                    ListNode temp = pCur.next.next;
                    pCur.next.next = null;
                    pCur.next = temp;
                    return head;
                }
            }
            pCur = pCur.next;
        }
        return head;
    }

    //O(1)时间,不遍历前序结点,复制覆盖
    private static ListNode deleteNode_1(ListNode head,ListNode toBeDeleted){
        if (head == null || toBeDeleted == null) {
            System.out.println("Invalid input.");
            return null;
        }

        //要删除不是尾结点
        if (toBeDeleted.next != null) {
            ListNode pNext =toBeDeleted.next;
            toBeDeleted.data = pNext.data;
            toBeDeleted.next = pNext.next;    //pNext结点并没释放
        }else if (head == toBeDeleted) {   //链表只有一个节点,删除头结点(也是尾结点)
            head = null;
        }else {   //链表中有多个节点,删除尾结点
            ListNode pNode = head;
            while (pNode.next != toBeDeleted){
                pNode = pNode.next;
            }
            pNode.next = null;
        }
        return head;
    }

    //打印链表
    private static void printList(ListNode head){
        if (head == null) {
            return;
        }
        while (head != null){
            System.out.print(" " +head.data);
            head = head.next;
        }
        System.out.println();
    }

    /*===================测试用例================*/
    private static void test(ListNode head,ListNode node){
        System.out.println("The original list is: ");
        printList(head);
//      System.out.println("The O(n) solution result is: ");
//      deleteNode_N(head, node);
//      printList(head);
        System.out.println("The O(1) solution result is: ");
        head = deleteNode_1(head, node);
        printList(head);

    }
    //链表有多个节点
    private static void test1() {
        ListNode first = new ListNode(5);
        ListNode second = new ListNode(4);
        ListNode third = new ListNode(3);
        ListNode fourth = new ListNode(2);

        first.next = second;
        second.next = third;
        third.next =fourth;

        test(first, first);
    }

    private static void test2() {
        ListNode first = new ListNode(5);
        ListNode second = new ListNode(4);
        ListNode third = new ListNode(3);
        ListNode fourth = new ListNode(2);

        first.next = second;
        second.next = third;
        third.next =fourth;

        test(first, second);        
    }

    private static void test3() {
        ListNode first = new ListNode(5);
        ListNode second = new ListNode(4);
        ListNode third = new ListNode(3);
        ListNode fourth = new ListNode(2);

        first.next = second;
        second.next = third;
        third.next =fourth;

        test(first, fourth);        
    }

    private static void test4() {
        ListNode first = new ListNode(5);

        test(first, first);
    }

    private static void test5() {
        test(null, null);       
    }

}
//构造链表
class ListNode{
    int data;
    ListNode next;
    public ListNode(int data){
        this.data = data;
        this.next = null;
    }
}


有不妥当之处,麻烦告知:D

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值