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

相关推荐
月华路7 分钟前
G1 新生代对象晋升老年代:实现机制与 GC 日志
java·jvm·算法
万法若空14 分钟前
排列组合恒等式
c++·算法
Nil20832 分钟前
leetcode 105从前序和中序遍历序列构造二叉树
算法·leetcode·职场和发展
一直C36 分钟前
【数据结构】哈希表+算法复杂度与经典排序查找(C语言)
java·linux·开发语言·数据结构·算法·ubuntu·散列表
paopaokaka_luck37 分钟前
基于springboot3+vue3的车间生产管理系统(Echarts图形化分析、BI报表)
java·前端·spring boot·学习·echarts
淡海水38 分钟前
04-02-哈希-Dictionary-TKey-TValue-上-核心数据结构
数据结构·算法·c#·哈希算法·编译·字典·dictionary
Canmag 兴隆磁性43 分钟前
自动磁场分布测试+镭雕设备 AT-C
学习·永磁材料·磁场分布测试
戴西软件44 分钟前
国内有哪些数据轻量化格式转换软件?
数据库·算法·安全·信息可视化·自动化·rpa
老洋葱Mr_Onion1 小时前
【C++】CSP-J初赛模拟卷七错题整理(作者自用)
c++·算法·深度优先
小小帅呀1 小时前
学习VLA第3天:训练一个最简单的神经网络
人工智能·神经网络·学习