【优选算法】链表:两数相加,两两交换节点,重排链表,合并K个升序链表,K个一组反转链表

文章目录

常用技巧与操作

技巧

  1. 多画图!!直观且形象,便于理解指向关系。
  2. 引入虚拟头结点,可以使代码统一,便于处理边界情况,也方便对链表操作
  3. 大胆创建局部变量。
    举个例子:在双向链表中插入新节点,需要写四行代码,并且顺序不能随便改,要保证链表不断开,思路很复杂。而引入了局部变量就价低了思考成本,避免链表断开的问题。

    4. 快慢双指针 是非常经典的解法,可以解决判断是否有环、找环入口、俩链表中倒数第N个节点等问题

操作

  1. 创建新节点new ListNode()
  2. 尾插: 创建尾指针tail维护链表最后一个节点,tail.next = node tail = node
  3. 头插: newhead.next = head.next head = newhead方便进行逆序链表操作

1. 两数相加(LC2)

两数相加

题目描述

解题思路

直接模拟两数相加过程

  1. 创建虚拟头结点head
  2. cur1cur2分别遍历两个链表,t保存cur1cur2指向数的和,结果链表中尾插t的末位
  3. 如果上一次产生了进位,剔除t的末位,此时剩余的1表示进位,t继续+= cur1 cur2指向的数.

代码解析

java 复制代码
class Solution {
    ListNode head = new ListNode();
    ListNode tail = head;

    //尾插
    private void addLast(int val){
        ListNode node = new ListNode(val);
        tail.next = node;
        tail = node;
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        int t = 0;
        while(cur1 != null && cur2 != null){
            t += cur1.val + cur2.val;
            addLast(t%10);
            t /= 10;
            cur1 = cur1.next;
            cur2 = cur2.next;
        }

        while(cur1 != null){
            t += cur1.val;
            addLast(t%10);
            t/=10;
            cur1 = cur1.next;
        }

        while(cur2 != null){
            t += cur2.val;
            addLast(t%10);
            t/=10;
            cur2 = cur2.next;
        }
        if(t==1)
            addLast(1);

        return head.next;
    }
}

注意: 如果最后t还为1,说明还有进位,要在结尾加上新节点,值为1.

拓展:链表相加二(NC40)

链表相加二

相加前需要把两个链表进行逆序操作,得到的结果也要逆序后返回。

java 复制代码
public class Solution { 
    //逆序
    private ListNode reverse(ListNode node){
        ListNode head = new ListNode(0);
        ListNode cur = node;
        while(cur!=null){
            ListNode newNode = new ListNode(cur.val);
            newNode.next = head.next;
            head.next = newNode;
            cur = cur.next;
        }
        return head.next;
    }
    
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        ListNode cur1  = reverse(head1);
        ListNode cur2  = reverse(head2);

        ListNode head = new ListNode(0);
        ListNode tail = head;
        int t = 0;
        while(cur1 != null || cur2 != null || t!=0){
            if(cur1 != null){
                t += cur1.val;
                cur1 = cur1.next;
            }
            if(cur2 != null){
                t += cur2.val;
                cur2 = cur2.next;
            }
            tail.next = new ListNode(t%10);
            tail = tail.next;
            t /= 10;
        }
        head = reverse(head.next);
        return head;
    }
}
相关推荐
菜鸡儿齐2 小时前
leetcode-组合总和
算法·leetcode·深度优先
滴滴答滴答答2 小时前
LeetCode Hot100 之 19 接雨水
算法·leetcode·职场和发展
网小鱼的学习笔记2 小时前
leetcode283移动零元素
java·开发语言·算法
weixin_448119942 小时前
Datawhale 大模型算法全栈基础篇 202602第2次笔记
笔记·算法
样例过了就是过了2 小时前
LeetCode热题100 反转链表
数据结构·算法·leetcode·链表
weixin_448119942 小时前
Datawhale 大模型算法全栈基础篇 202602第3次笔记
笔记·rnn·算法
紫陌涵光2 小时前
538. 把二叉搜索树转换为累加树
c++·算法·leetcode
Zik----2 小时前
Leetcode35 —— 搜索插入位置(二分查找)
数据结构·算法·leetcode
yi.Ist2 小时前
牛客寒假训练营3
c++·学习·算法