《程序员代码面试指南》-第二章 链表问题1

1. 打印两个有序链表的公共部分

题目:
给定两个有序链表的头指针head1和head2,打印两个链表的公共部分。

代码:

package charpter2;
import java.util.List;
public class test1 {
    public static void getPublic(ListNode head1, ListNode head2) {
        while (head1 != null && head2 != null) {
            if (head1.val > head2.val) {
                head2 = head2.next;
            } else if (head1.val < head2.val) {
                head1 = head1.next;
            } else {
                System.out.println(head1.val);
                head1 = head1.next;
                head2 = head2.next;
            }
        }
    }
    public static void main(String[] args) {
        ListNode listNode11 = new ListNode(1);
        ListNode listNode12 = new ListNode(2);
        ListNode listNode13 = new ListNode(3);
        ListNode listNode14 = new ListNode(4);
        ListNode listNode15 = new ListNode(5);
        listNode11.next = listNode12;
        listNode12.next = listNode13;
        listNode13.next = listNode14;
        listNode14.next = listNode15;
        ListNode listNode21 = new ListNode(2);
        ListNode listNode22 = new ListNode(3);
        ListNode listNode23 = new ListNode(4);
        ListNode listNode24 = new ListNode(5);
        ListNode listNode25 = new ListNode(6);
        listNode21.next = listNode22;
        listNode22.next = listNode23;
        listNode23.next = listNode24;
        listNode24.next = listNode25;
        getPublic(listNode11, listNode21);
    }
}

2. 在单链表和双链表中删除倒数第K个节点

题目:
分别实现两个函数,一个可以删除单链表中倒数第K个节点,另一个可以删除双链表中倒数第K个节点
要求:
如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1).

代码:

package charpter2;

import java.util.List;

public class test2 {

    // 我的解法
    public static ListNode deleteSingleKth(ListNode head, int K) {
        ListNode temp = null;
        ListNode head1 = head;
        ListNode res = head1;
        int n = 1;
        while (head != null) {
            if (n == K + 2) {
                temp = head;
                break;
            }
            head = head.next;
            n++;
        }

        System.out.println("temp:" + temp.val);
        while (temp != null) {
            temp = temp.next;
            head1 = head1.next;
        }

        System.out.println("head1:" + head1.val);

        head1.next = head1.next.next;

        return res;
    }

    // 最优解
    public static ListNode deleteSingleKth1(ListNode head, int K) {
        if (head == null || K < 1) {
            return null;
        }
        ListNode cur = head;
        while (cur != null) {
            cur = cur.next;
            K--;
        }

        if (K == 0) {
            head = head.next;
        }

        if (K > 0) {
            head = null;
        }

        if (K < 0) {
            cur = head;
            while (++K != 0) {
                cur = cur.next;
            }
            head.next = head.next.next;
        }

        return head;
    }


    public static void main(String[] args) {
        ListNode listNode11 = new ListNode(1);
        ListNode listNode12 = new ListNode(2);
        ListNode listNode13 = new ListNode(3);
        ListNode listNode14 = new ListNode(4);
        ListNode listNode15 = new ListNode(5);
        listNode11.next = listNode12;
        listNode12.next = listNode13;
        listNode13.next = listNode14;
        listNode14.next = listNode15;

        ListNode ss = deleteSingleKth1(listNode11, 3);

        while (ss != null) {
            System.out.println(ss.val);
            ss = ss.next;
        }
    }
}

对于双链表的调整,几乎与单链表的处理方式一样,注意last指针的重连即可。

3. 删除链表的中间节点

题目:
给定链表的头节点head, 实现删除链表的中间节点的函数

代码:

package charpter2;

import java.util.List;

public class test3 {

    // 我的解法
    public static ListNode deleteMid(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        if (head.next.next == null) {
            return head.next;
        }
        int k = 0;
        ListNode cur = head;
        while (cur != null) {
            cur = cur.next;
            k++;
        }

        int mid = 0;
        if (k % 2 == 0) {
            mid = k / 2 - 2;
        } else {
            mid = k / 2 - 1;
        }

        ListNode temp = head;
        ListNode res = temp;
        while (head != null) {
            if (mid == 0) {
                temp = head;
                break;
            } else {
                head = head.next;
                mid--;
            }
        }
        temp.next = temp.next.next;
        return res;
    }

    // 最优解
    public static ListNode deleteMidNode(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        if (head.next.next == null) {
            return head.next;
        }

        ListNode pre = head;
        ListNode cur = pre.next.next;

        while (cur.next != null && cur.next.next != null) {
            pre = pre.next;
            cur = cur.next.next;
        }
        pre.next = pre.next.next;
        return head;
    }

    public static void main(String[] args) {
        ListNode listNode11 = new ListNode(1);
        ListNode listNode12 = new ListNode(2);
        ListNode listNode13 = new ListNode(3);
        ListNode listNode14 = new ListNode(4);
        ListNode listNode15 = new ListNode(5);

        listNode11.next = listNode12;
        listNode12.next = listNode13;
        listNode13.next = listNode14;
        listNode14.next = listNode15;

        ListNode aa = deleteMidNode(listNode11);
        System.out.println();
        while (aa != null) {
            System.out.println(aa.val);
            aa = aa.next;
        }
    }
}

4. 反转单向链表

题目:
实现反转链表的函数
要求:
如果链表长度为N,时间复杂度要求为O(N), 额外空间复杂度要求为O(1)

代码:

package charpter2;
public class test4 {
    public static ListNode reverseNode(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;

        while (head != null) {
            cur = head.next;
            head.next = pre;
            pre = head;
            head = cur;
        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode listNode11 = new ListNode(1);
        ListNode listNode12 = new ListNode(2);
        ListNode listNode13 = new ListNode(3);
        ListNode listNode14 = new ListNode(4);
        ListNode listNode15 = new ListNode(5);

        listNode11.next = listNode12;
        listNode12.next = listNode13;
        listNode13.next = listNode14;
        listNode14.next = listNode15;

        ListNode res = reverseNode(listNode11);
        while (res != null) {
            System.out.println(res.val);
            res = res.next;
        }
    }
}

5. 反转部分单向链表

题目:
给定一个单向链表的头节点head,以及两个整数from和to,在单向链表上把第from个节点到第to个节点这一部分进行反转。
代码:

package charpter2;

import java.util.Stack;

public class test5 {
    public static ListNode reversePartNode(ListNode head, int from, int to) {
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        int k = 1;
        // 将需要反转的节点放入栈中
        while (head != null) {
            if (k >= from && k <= to) {
                stack.push(head);
                head = head.next;
                k++;
            } else if (k < from) {
                head = head.next;
                k++;
            } else {
                break;
            }
        }
        if (from > to || from < 1 || to > k - 1) {
            return head;
        }
        int n = 1;
        if (from == 1) {
            cur = stack.pop();
        }
        ListNode res = cur;
        while (n < from - 1) {
            cur = cur.next;
            n++;
        }
        while (!stack.empty()) {
            cur.next = stack.pop();
            cur = cur.next;
        }
        cur.next = head;
        return res;
    }

    public static void main(String[] args) {
        ListNode listNode11 = new ListNode(1);
        ListNode listNode12 = new ListNode(2);
        ListNode listNode13 = new ListNode(3);
        ListNode listNode14 = new ListNode(4);
        ListNode listNode15 = new ListNode(5);
        
        listNode11.next = listNode12;
        listNode12.next = listNode13;
        listNode13.next = listNode14;
        listNode14.next = listNode15;

        ListNode res = reversePartNode(listNode11, 3, 5);
        System.out.println();
        while (res != null) {
            System.out.println(res.val);
            res = res.next;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值