力扣-Hot100-链表其二【算法学习day.35】

前言

###我做这类文档一个重要的目的还是给正在学习的大家提供方向(例如想要掌握基础用法,该刷哪些题?)我的解析也不会做的非常详细,只会提供思路和一些关键点,力扣上的大佬们的题解质量是非常非常高滴!!!


习题

1.合并两个有序链表

题目链接: 21. 合并两个有序链表 - 力扣(LeetCode)

题面:

**基本分析:**双指针

代码:

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode head = new ListNode(0);
        ListNode node = new ListNode(0);
        head.next=node;
        while(list1!=null&&list2!=null){
            if(list1.val>list2.val){
            ListNode flag = new ListNode(list2.val);
            node.next = flag;
            node = node.next;
            list2 = list2.next;
            }else{
            ListNode flag = new ListNode(list1.val);
            node.next = flag;
            node = node.next;
            list1 = list1.next;
            }
        }
        if(list1!=null){
           node.next = list1;
        }
        if(list2!=null){
            node.next = list2;
        }
        return head.next.next;
    }
}

2.两数相加

题目链接: 2. 两数相加 - 力扣(LeetCode)

题面:

基本分析:双指针

代码:

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode node = new ListNode(0);
        head.next = node;
        int flag = 0;
        while(l1!=null&&l2!=null){
            int sum = l1.val+l2.val;
            sum+=flag;
            flag = sum/10;
            sum = sum%10;
            ListNode flag2 = new ListNode(sum);
            node.next = flag2;
            node = node.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        if(l1!=null){
            while(l1!=null){
            int sum = l1.val;
            sum+=flag;
            flag = sum/10;
            sum = sum%10;
            ListNode flag2 = new ListNode(sum);
            node.next = flag2;
            node = node.next;
            l1 = l1.next;
            }
        }
         if(l2!=null){
            while(l2!=null){
            int sum = l2.val;
            sum+=flag;
            flag = sum/10;
            sum = sum%10;
            ListNode flag2 = new ListNode(sum);
            node.next = flag2;
            node = node.next;
            l2 = l2.next;
            }
        }
        if(flag>0){
            ListNode flag2 = new ListNode(flag);
            node.next = flag2;
            node = node.next; 
        }
        return head.next.next;
    }

}

3.k个一组翻转链表

题目链接: 25. K 个一组翻转链表 - 力扣(LeetCode)

题面:

**基本分析:**我的做法是先把要反转的存起来,然后构建链表,不符合题目下方的要求,可以看看大佬的题解

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode root = new ListNode(0);
        ListNode node = new ListNode(0);
        root.next = node;
        int count = 0;
        List<Integer> list = new ArrayList<>();
        while(head!=null){
         list.add(head.val);
         count++;
         head = head.next;
         if(count%k==0){
            for(int i = list.size()-1;i>=0;i--){
                ListNode flag = new ListNode(list.get(i));
                node.next = flag;
                node = node.next;
            }
            list.clear();
            count = 0;
         } 
        }
        if(count>0){
            for(int i = 0;i<list.size();i++){
                ListNode flag = new ListNode(list.get(i));
                node.next = flag;
                node = node.next;
            } 
        }
        return root.next.next;
    }
    
}

后言

上面是力扣Hot100的链表专题,下一篇是该专题的其他题目,希望有所帮助,一同进步,共勉!

相关推荐
qq_348231851 小时前
大模型算法: 反向传播
算法
电科_银尘1 小时前
【大模型算法与工程 27】训练配方:批量、学习率、精度与梯度裁剪
学习·算法·机器学习
专注仿真2 小时前
智能体训练之模仿学习:GAIL 算法详解与实战
python·算法·仿真·多智能体强化学习·afsim智能体训练
leobertlan4 小时前
痛苦系列 | DSP-04 截断时空的“窗”:FIR 滤波器设计与线性相位
算法
秋天的怀念_David5 小时前
算法一千零一夜 · 第三十五夜 · 恢复风暴
java·程序人生·算法
泯泷7 小时前
别再“裸聊”大模型了!你家的 AI 为什么总胡说八道?根子在 RAG
人工智能·算法·架构
fly_sunnn8 小时前
机械狗的前置学习(python与Linux基础操作)
学习
2601_967980118 小时前
Python中heapq模块的各种用法实例
数据结构·python·算法·应用实例·heapq模块
漂流瓶jz10 小时前
UVA-1615 高速公路 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·贪心算法·贪心·aoapc·算法竞赛入门经典·uva