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;
    }
}
相关推荐
noipp6 小时前
推荐题目:洛谷 P10907 [蓝桥杯 2024 国 B] 蚂蚁开会
c语言·c++·算法·编程·洛谷
kyriewen7 小时前
Git Commit 前自动修复代码风格?配置 Husky + lint-staged,从此 CR 只聊逻辑
前端·git·面试
程序员二叉7 小时前
【JUC】ThreadLocal底层原理|内存泄漏|弱引用|跨线程传递方案
java·开发语言·面试·职场和发展·juc
程序员二叉7 小时前
【JUC】线程池全套深度详解|参数|流程|拒绝策略|调优|异常处理
java·开发语言·jvm·算法·面试·juc
青山木7 小时前
Hot 100 --- 轮转数组
java·数据结构·算法
徐小夕8 小时前
Loop Engineering 深度解析与实战指南(全网最全)
前端·算法·github
北域码匠9 小时前
SHA-1算法:安全哈希原理与应用解析
算法·c#·哈希算法
手写码匠9 小时前
手写 GraphRAG:从零实现图增强检索增强生成系统
人工智能·深度学习·算法·aigc
BomanGe19 小时前
NSK重载高刚性滚珠丝杠技术详解
经验分享·算法·规格说明书
Matrix_1110 小时前
手机里的计算摄影:广角形变校正算法
人工智能·算法·智能手机·计算摄影