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;
    }
}
相关推荐
ShineWinsu7 小时前
对于C++:IO流状态的详细解析
c++·面试·io·cin·io流状态·goodbit·failbit
「PlanA」7 小时前
测试开发面经001
面试
数模竞赛Paid answer8 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆8 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
JL158 小时前
面试项目被问到自闭-如何把课设包装成亮眼项目
面试·职场和发展
想吃火锅100510 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
Nil20810 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
依然鸣11 小时前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论
库玛西13 小时前
现代 C++ 智能指针全景指南:从 RAII 思想到工业级实践
c语言·开发语言·c++·笔记·面试
Jackson__14 小时前
面试官:如何在老项目中使用新框架,并实现通信?
前端·javascript·面试