力扣-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的链表专题,下一篇是该专题的其他题目,希望有所帮助,一同进步,共勉!

相关推荐
STLearner23 分钟前
ICML 2026 | LLM×Graph论文总结[2]【Graph4LLM,Graph4Agent,智能体记忆(Memory)
大数据·人工智能·python·深度学习·学习·机器学习·数据挖掘
其实防守也摸鱼2 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·运维·网络·人工智能·学习·安全·macos
YM52e6 小时前
鸿蒙Flutter Padding内边距:EdgeInsets详解
android·学习·flutter·华为·harmonyos·鸿蒙
m沐沐8 小时前
【深度学习】YOLOv2目标检测算法——改进点、网络结构与聚类先验框解析
人工智能·pytorch·深度学习·算法·yolo·目标检测·transformer
不会就选b8 小时前
算法日常・每日刷题--<链表>3
数据结构·算法·链表
geovindu8 小时前
go: Iterative Algorithms
开发语言·后端·算法·golang·迭代算法
西安老张(AIGC&ComfyUI)8 小时前
第034章:ComfyUI&AIGC一阶段学习总结及下阶段学习安排
学习·aigc
稚南城才子,乌衣巷风流10 小时前
块状链表:数据结构详解与实现
数据结构·链表
Zachery Pole10 小时前
CCF-CSP备战NO.7【队列】
算法
闪电悠米11 小时前
力扣hot100-48.旋转图像-转置翻转详解
算法·leetcode·职场和发展