【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;
        }
    }
}
相关推荐
计算机小白一个4 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^5 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
大数据追光猿7 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!7 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉7 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生7 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴7 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
Leuanghing7 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
qy发大财7 小时前
加油站(力扣134)
算法·leetcode·职场和发展
王老师青少年编程7 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛