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;
    }
}
相关推荐
2401_87622134几秒前
因数个数、因数和、因数积
c++·算法
云里雾里!10 分钟前
LeetCode 744. 寻找比目标字母大的最小字母 | 从低效到最优的二分解法优化
算法·leetcode
CCPC不拿奖不改名13 分钟前
网络与API:HTTP基础+面试习题
网络·python·网络协议·学习·http·面试·职场和发展
无限码力15 分钟前
华为OD技术面真题 - 计算机网络 - 3
计算机网络·华为od·面试·华为od技术面真题·华为od面试八股文·华为od技术面计算机网络相关
Bigbig.21 分钟前
驱动工程师面试题 - 操作系统1
linux·开发语言·面试·硬件架构
一条大祥脚25 分钟前
26.1.3 快速幂+容斥 树上dp+快速幂 带前缀和的快速幂 正序转倒序 子序列自动机 线段树维护滑窗
数据结构·算法
二狗哈29 分钟前
czsc入门5: Tick RawBar(原始k线) NewBar (新K线)
算法·czsc
Tisfy35 分钟前
LeetCode 0865.具有所有最深节点的最小子树:深度优先搜索(一次DFS + Python5行)
算法·leetcode·深度优先·dfs·题解
Q741_14739 分钟前
C++ 队列 宽度优先搜索 BFS 力扣 429. N 叉树的层序遍历 C++ 每日一题
c++·算法·leetcode·bfs·宽度优先
Yzzz-F40 分钟前
P4145 上帝造题的七分钟 2 / 花神游历各国[线段树 区间开方(剪枝) + 区间求和]
算法·机器学习·剪枝