【CT】LeetCode手撕—21. 合并两个有序链表

目录

  • 题目
  • 1-思路
  • [2- 实现](#2- 实现)
    • [⭐21. 合并两个有序链表------题解思路](#⭐21. 合并两个有序链表——题解思路)
  • [3- ACM实现](#3- ACM实现)

题目


1-思路

双指针:题目提供的 list1 和 list2 就是两个双指针

  • 通过每次移动 list1 和 list2 并判断二者的值,判断完成后将其 插入到新的当前结点 cur.next 后即可完成链表按升序排列。

2- 实现

⭐21. 合并两个有序链表------题解思路

java 复制代码
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        // 定义一个 头结点记录位置
        ListNode dummyHead = new ListNode(0);
        ListNode cur = dummyHead;
        while(list1!=null && list2!=null){
            if(list1.val<list2.val){
                cur.next = list1;
                list1 = list1.next;
            }else{
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }

        if(list1!=null){
            cur.next = list1;
        }
        if(list2!=null){
            cur.next = list2;
        }
        return dummyHead.next;
    }
}

3- ACM实现

java 复制代码
public class twoLinkASC {

    static class ListNode{
        int val;
        ListNode next;
        ListNode(){}
        ListNode(int x){
            val = x;
        }
    }

    public static ListNode mergeTwoLink(ListNode list1,ListNode list2){
        // 定义虚拟头
        ListNode dummyHead = new ListNode(-1);
        ListNode cur = dummyHead;

        while(list1!=null && list2!=null){
            if(list1.val<list2.val){
                cur.next = list1;
                list1 = list1.next;
            }else{
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        if(list1!=null){
            cur.next = list1;
        }
        if(list2!=null){
            cur.next = list2;
        }
        return dummyHead.next;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入链表1的长度");
        ListNode head1=null,tail1=null;
        int n1 = sc.nextInt();
        System.out.println("输入链表元素");
        for(int i = 0 ; i < n1;i++){
            ListNode nowNode = new ListNode(sc.nextInt());
            if(head1==null){
                head1 = nowNode;
                tail1 = nowNode;
            }else{
                tail1.next = nowNode;
                tail1 = nowNode;
            }
        }

        System.out.println("输入链表2的长度");
        ListNode head2=null,tail2=null;
        int n2 = sc.nextInt();
        System.out.println("输入链表元素");
        for(int i = 0 ; i < n2;i++){
            ListNode nowNode = new ListNode(sc.nextInt());
            if(head2==null){
                head2 = nowNode;
                tail2 = nowNode;
            }else{
                tail2.next = nowNode;
                tail2 = nowNode;
            }
        }

        ListNode forRes = mergeTwoLink(head1,head2);
        while (forRes!=null){
            System.out.print(forRes.val+" ");
            forRes = forRes.next;
        }
    }
}
相关推荐
凌辰揽月32 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
lifallen38 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
jingfeng51439 分钟前
数据结构排序
数据结构·算法·排序算法
能工智人小辰1 小时前
Codeforces Round 509 (Div. 2) C. Coffee Break
c语言·c++·算法
kingmax542120081 小时前
CCF GESP202503 Grade4-B4263 [GESP202503 四级] 荒地开垦
数据结构·算法
岁忧1 小时前
LeetCode 高频 SQL 50 题(基础版)之 【高级字符串函数 / 正则表达式 / 子句】· 上
sql·算法·leetcode
eachin_z2 小时前
力扣刷题(第四十九天)
算法·leetcode·职场和发展
闻缺陷则喜何志丹2 小时前
【强连通分量 缩点 拓扑排序】P3387 【模板】缩点|普及+
c++·算法·拓扑排序·洛谷·强连通分量·缩点
机器学习之心3 小时前
机器学习用于算法交易(Matlab实现)
算法·机器学习·matlab
AL流云。3 小时前
【优选算法】C++滑动窗口
数据结构·c++·算法