【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;
        }
    }
}
相关推荐
远瞻。3 小时前
【论文阅读】人脸修复(face restoration ) 不同先验代表算法整理2
论文阅读·算法
先做个垃圾出来………6 小时前
哈夫曼树(Huffman Tree)
数据结构·算法
phoenix@Capricornus8 小时前
反向传播算法——矩阵形式递推公式——ReLU传递函数
算法·机器学习·矩阵
Inverse1628 小时前
C语言_动态内存管理
c语言·数据结构·算法
数据与人工智能律师8 小时前
虚拟主播肖像权保护,数字时代的法律博弈
大数据·网络·人工智能·算法·区块链
wuqingshun3141598 小时前
蓝桥杯 16. 外卖店优先级
c++·算法·职场和发展·蓝桥杯·深度优先
YouQian7729 小时前
2025春训第十九场
算法
CodeJourney.9 小时前
基于MATLAB的生物量数据拟合模型研究
人工智能·爬虫·算法·matlab·信息可视化
Epiphany.5569 小时前
素数筛(欧拉筛算法)
c++·算法·图论
爱吃涮毛肚的肥肥(暂时吃不了版)9 小时前
项目班——0510——JSON网络封装
c++·算法·json