编程导航算法通关第2关|白银教程学习

链表反转常见拓展面试题

本章主要是基于青铜教程中处理链表反转的问题拔高。包括指定区间反转、两两交换链表中的结点,单链表加1、链表加法。


指定区间反转

指定区间反转
给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

  1. 头插法。整体的思路是在需要反转的区间,遍历到每一个结点,让这个新节点来到反转部分的起始位置。【本质】:带头结点的反转。

  1. 穿针引线法。 先将待反转的区域反转; 将区间左边临近的节点的next指针指向反转后的头结点。将反转后的尾结点的next指针指向区间右边的相邻结点。【寻找前驱后继的结点】

// 头插法
public static ListNode reverseBetween2(ListNode head, int left, int right) {
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode pre = dummyNode;
        for (int i = 0; i < left - 1; i++) {
            pre = pre.next;
        }
        // pre 作为定点,类似于虚拟头结点
        ListNode cur = pre.next;
        ListNode next;
        for (int i = 0; i < right - left; i++) {
            next = cur.next;
            cur.next = next.next; //更新
            next.next = pre.next;
            pre.next = next;
        }
        return dummyNode.next;
    }
// 穿线引线法。说白了,就是将子链表反转,再介入其中。
// 那么,也就是说应该要实现子功能 反转链表、处理头部和尾部

public static ListNode  reverseList(ListNode head) {
        ListNode pre = null;  // 新链表的头结点(实际意义)
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

public static ListNode reverseBetween(ListNode head, int left, int right) {
        // 寻找前驱后继的节点。
        // 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;

        ListNode pre = dummyNode;
        // 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点
        for (int i = 0; i < left - 1; i++) {
            pre = pre.next;
        }
        // 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点
        ListNode rightNode = pre;
        for (int i = 0; i < right - left + 1; i++) {
            rightNode = rightNode.next;
        }

        // 第 3 步:切断出一个子链表(截取链表)
        ListNode leftNode = pre.next;
        ListNode rightNext = rightNode.next;

        // 注意:切断链接
//        pre.next = null;
        rightNode.next = null;

        // 第 4 步:反转链表的子区间
        reverseList(leftNode);

        // 第 5 步:接回到原来的链表中
        pre.next = rightNode;
        leftNode.next = rightNext;
        return dummyNode.next;
    }

两两交换链表中的结点

交换相邻结点
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

  1. 题目分析:成对的反转。那么,要研究如何调整每个结点的指向。

public static  ListNode swapPairs(ListNode head) {
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode cur = dummyHead;
        while (cur.next != null && cur.next.next != null) {
            ListNode node1 = cur.next;
            ListNode node2 = cur.next.next;
            cur.next = node2;
            node1.next = node2.next;
            node2.next = node1;
            cur = node1;
        }
        return dummyHead.next;
    }

单链表加 1

单链表加一
用一个非空单链表来表示一个非负整数,然后将这个整数加一。你可以假设这个整数除了 0 本身,没有任何前导的 0。这个整数的各个数位按照 高位在链表头部、低位在链表尾部 的顺序排列。

  1. 题目分析:模拟加法时,从低位开始计算,那么需要将链表反转;这里可以使用栈 | 链表。
  2. 利用栈。将链表元素压栈,弹出的元素开始加法,同时需要考虑进位以影响下一位的值。
  3. 利用链表反转。反转计算,再反转得到结果。
// 利用栈
public static ListNode plusOne(ListNode head) {
    Stack<Integer> stack = new Stack();
    while (head != null) {
        stack.push(head.val);
        head = head.next;
    }
    int carry = 0;
    ListNode dummy = new ListNode(0);
//        int adder = 1;
    while (!stack.empty() || carry > 0) {
        int digit = stack.empty() ? 0 : stack.pop();
        int sum = digit + 1 + carry;
        carry = sum >= 10 ? 1 : 0;
        sum = sum >= 10 ? sum - 10 : sum;
        ListNode cur = new ListNode(sum);
        cur.next = dummy.next;
        dummy.next = cur;
//            adder = 0;
    }
    return dummy.next;
}

链表加法

链表的两数相加
给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。

  1. 题目分析:与上面一道题类似。都可以用栈、链表解决。注意进制方面的处理即可。
// 利用栈
public static ListNode addInListByStack(ListNode head1, ListNode head2) {
        Stack<ListNode> stack1 = new Stack<ListNode>();
        Stack<ListNode> stack2 = new Stack<ListNode>();
        while (head1 != null) {
            stack1.push(head1);
            head1 = head1.next;
        }
        while (head2 != null) {
            stack2.push(head2);
            head2 = head2.next;
        }
        ListNode newHead = new ListNode(-1);
        int carry = 0;
        //这里设置carry!=0,是因为当st1,st2都遍历完时,如果carry=0,就不需要进入循环了
        while (!stack1.empty() || !stack2.empty() || carry != 0) {
            ListNode a = new ListNode(0);
            ListNode b = new ListNode(0);
            if (!stack1.empty()) {
                a = stack1.pop();
            }
            if (!stack2.empty()) {
                b = stack2.pop();
            }
            //每次的和应该是对应位相加再加上进位
            int get_sum = a.val + b.val + carry;
            //对累加的结果取余
            int ans = get_sum % 10;
            //如果大于0,就进位
            carry = get_sum / 10;
            ListNode cur = new ListNode(ans);
            cur.next = newHead.next;
            //每次把最新得到的节点更新到neHead.next中
            newHead.next = cur;
        }
        return newHead.next;
    }

// 利用链表反转
private static ListNode reverse(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
public static ListNode addInListByReverse(ListNode head1, ListNode head2) {
        head1 = reverse(head1);
        head2 = reverse(head2);
        ListNode head = new ListNode(-1);
        ListNode cur = head;
        int carry = 0;
        while (head1 != null || head2 != null) {
            int val = carry;
            if (head1 != null) {
                val += head1.val;
                head1 = head1.next;
            }
            if (head2 != null) {
                val += head2.val;
                head2 = head2.next;
            }
            cur.next = new ListNode(val % 10);
            carry = val / 10;
            cur = cur.next;
        }
        if (carry > 0) {
            cur.next = new ListNode(carry);
        }
        return reverse(head.next);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值