【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;
        }
    }
}
相关推荐
Java致死32 分钟前
费马小定理
算法·费马小定理
不吃元西1 小时前
leetcode 74. 搜索二维矩阵
算法·leetcode·矩阵
小开不是小可爱1 小时前
leetcode_454. 四数相加 II_java
java·数据结构·算法·leetcode
aaaweiaaaaaa2 小时前
蓝桥杯c ++笔记(含算法 贪心+动态规划+dp+进制转化+便利等)
c语言·数据结构·c++·算法·贪心算法·蓝桥杯·动态规划
Hesse2 小时前
希尔排序:Python语言实现
python·算法
h^hh3 小时前
pipe匿名管道实操(Linux)
数据结构·c++·算法
dr李四维3 小时前
解决缓存穿透的布隆过滤器与布谷鸟过滤器:谁更适合你的应用场景?
redis·算法·缓存·哈希算法·缓存穿透·布隆过滤器·布谷鸟过滤器
亓才孓3 小时前
[leetcode]01背包问题
算法·leetcode·职场和发展
学习编程的gas3 小时前
数据结构——二叉树
数据结构·算法
its_a_win4 小时前
蓝桥杯 2023省B 飞机降落 dfs
c++·算法·蓝桥杯