编程导航第二关-白银-链表反转拓展问题

1.指定区间反转

1.1.头插法

使用虚拟头节点来做,也叫头插法。就是将反转区间的节点拼接到pre的后面。

 public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode pre = dummyNode;
        for(int i = 1;i < left;i++){
            pre = pre.next;
        }
        ListNode cur = pre.next;
        ListNode next;
        for(int i = 0;i < right - left;i++){
            // 暂存cur的后继
            next = cur.next;
            // 修改cur后继
            cur.next = cur.next.next;
            // 修改next的前驱和后继
            next.next = pre.next; // 后,插入到pre后面
            pre.next = next;
        }
        return dummyNode.next;
    }

无论是反转整条链表还是反转指定区间的链表,两者对于指向的修改略有不同,但是我们都需要知道三个节点:pre,cur,cur.next,在pre的后面拼接上节点。

为什么指向的修改会出现差异?

反转整条链表中cur是一直在往后移的,也就是说是将cur拼接到pre后面。

而在区间反转中,cur从头到尾都是指向了left处的节点,拼接到pre后面的节点是cur.next。

1.2.穿针引线法

对比头插法,感觉更容易理解,将区间内的一段链表切割出来,同时记录下left的前驱,right的后继,便于反转后修改指向。

至于反转的方法就是跟反转整条链表一样了。这里用穿针引线来反转。

 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 循环里,语义清晰
        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 succ = rightNode.next;

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

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

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

    /**
     * 基本的反转方法
     *
     * @param head
     * @return
     */
    public static ListNode  reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }

2.两两交换链表中的节点

最容易理解的就是依次两两交换两节点

public ListNode swapPairs(ListNode head) {
    //   if(head == null || head.next == null) return head;
    //   ListNode next = head.next;
    //   ListNode newNode =swapPairs(next.next);
    //   next.next = head;
    //   head.next = newNode;
    //   return next;
    ListNode dummyNode = new ListNode(-1);
    dummyNode.next = head;
    ListNode temp = dummyNode;
    while(temp.next != null && temp.next.next != null){
        ListNode node1 = temp.next;
        ListNode node2 = temp.next.next;
        // 修改指向
        temp.next = node2;
        node1.next = node2.next;
        node2.next = node1;
        // 移动到下一个交换区间
        temp = node1;
    }
    return dummyNode.next;
    }

3.链表中的两数相加

3.1.栈

image-20230721190931153

因为存在进位的情况,所以我们需要进行逆序相加,也就是要反转链表。使用栈来做,十分容易理解。

 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<Integer> s1 = new Stack<>();
        Stack<Integer> s2 = new Stack<>();
        while(l1 != null){
            s1.push(l1.val);
            l1 = l1.next;
        }
         while(l2 != null){
            s2.push(l2.val);
            l2 = l2.next;
        }
        ListNode dummuNode = new ListNode(-1);
        // 进位
        int carry = 0;
        while(!s1.isEmpty() || !s2.isEmpty() || carry != 0){
            int a = 0,b = 0;
            if(!s1.isEmpty()){
                 a = s1.pop();
            }
             if(!s2.isEmpty()){
                 b = s2.pop();
            }
            // 这里需要加上进位
            int res = a + b + carry;
            // 取余
            int ans = res % 10;
            // 求进位
            carry = res / 10;
            // 新建节点
            ListNode cur = new ListNode(ans);
            // 这里是从后往前构造链表
            cur.next = dummuNode.next;
            dummuNode.next = cur;
        }

        return dummuNode.next;
    }
3.2.头插或者穿针引线反转

就是用之前的基本链表反转方法来把两条链表进行反转,在进行加和运算,最后得到的链表需要再次进行反转。

注意:不要忘了最后一个元素相加时可能还会有进位,要进行判断

 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);
    }

    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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值