day36(12.17)——leetcode面试经典150

21. 合并两个有序链表

21. 合并两个有序链表

我的微服务还没正式开启呢,我这两天在做一个贼简单的网站,昨天写的很兴奋,写到三点,整的今天巨困!!!

题目:

题解:

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 cur = new ListNode(0);
        ListNode list = cur;
        while(list1!=null && list2!=null) {
            if(list1.val<list2.val) {
                cur.next = new ListNode(list1.val);
                list1 = list1.next; 
            }
            else {
                cur.next = new ListNode(list2.val);
                list2 = list2.next;
            }
            cur = cur.next;
        }
        while(list1 != null) {
            cur.next = new ListNode(list1.val);
            list1 = list1.next;
            cur = cur.next;
        }
        while(list2 != null) {
            cur.next = new ListNode(list2.val);
            list2 = list2.next;
            cur = cur.next;
        }
        return list.next;
    }
}

还有另一种写法,思路差不多

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 cur = new ListNode(0);
        ListNode list = cur;
        while(list1!=null || list2!=null) {
            if(list1 == null && list2 != null) {
                cur.next = new ListNode(list2.val);
                list2 = list2.next;
            }
            else if(list1 != null && list2 == null) {
                cur.next = new ListNode(list1.val);
                list1 = list1.next;
            }
            //只剩下两个都存在的情况了
            //两个都不存在就进来循环了
            else {
                if(list1.val<list2.val) {
                    cur.next = new ListNode(list1.val);
                    list1 = list1.next; 
                }
                else {
                    cur.next = new ListNode(list2.val);
                    list2 = list2.next;
                }
            }
            cur = cur.next;
        }
        return list.next;
    }
}
相关推荐
铉铉这波能秀12 小时前
LeetCode Hot100数据结构背景知识之字典(Dictionary)Python2026新版
数据结构·python·算法·leetcode·字典·dictionary
蜡笔小马12 小时前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
我是咸鱼不闲呀12 小时前
力扣Hot100系列20(Java)——[动态规划]总结(下)( 单词拆分,最大递增子序列,乘积最大子数组 ,分割等和子集,最长有效括号)
java·leetcode·动态规划
唐梓航-求职中12 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
仟濹12 小时前
【算法打卡day3 | 2026-02-08 周日 | 算法: BFS】3_卡码网99_计数孤岛_BFS | 4_卡码网100_最大岛屿的面积DFS
算法·深度优先·宽度优先
Ll130452529812 小时前
Leetcode二叉树part4
算法·leetcode·职场和发展
颜酱12 小时前
二叉树遍历思维实战
javascript·后端·算法
宝贝儿好12 小时前
第二章: 图像处理基本操作
算法
小陈phd12 小时前
多模态大模型学习笔记(二)——机器学习十大经典算法:一张表看懂分类 / 回归 / 聚类 / 降维
学习·算法·机器学习
@––––––13 小时前
力扣hot100—系列4-贪心算法
算法·leetcode·贪心算法