【Hot100】LeetCode—21. 合并两个有序链表



1- 思路

思路

思路如下 :①定义四个指针、②通过判断两个 list的指针结果 、③移动 三个指针来收集结果

  • ① 定义四个指针
    • curAcurB 分别用来遍历 list1list2
    • dummyHead 用来记录头结点
    • cur 用来记录合并后的链表的当前结点
  • ② 利用 while 循环遍历两个链表:移动三个指针
  • ③ 最终结果,判断curAcurB哪个未遍历完,直接拼接在 cur 后面即可

2- 实现

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

在这里插入图片描述

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode curA = list1;
        ListNode curB = list2;

        ListNode dummyHead = new ListNode(-1);
        ListNode cur = dummyHead;
        while(curA!=null && curB!=null){
            if(curA.val < curB.val){
                cur.next = curA;
                curA = curA.next;
            }else{
                cur.next = curB;
                curB = curB.next;
            }
            cur = cur.next;
        }
        if(curA !=null){
            cur.next = curA;
        }
        if(curB !=null){
            cur.next = curB;
        }
        return dummyHead.next;
    }
}

3- ACM 实现

public class mergeList {

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

    public static ListNode mergeList(ListNode list1,ListNode list2){
        // 1.数据结构
        ListNode curA = list1;
        ListNode curB = list2;
        ListNode dummyHead = new ListNode(0);
        ListNode cur = dummyHead;
        while(curA!=null && curB!=null){
            if(curA.val < curB.val){
                cur.next = curA;
                curA = curA.next;
            }else{
                cur.next = curB;
                curB = curB.next;
            }
            cur = cur.next;
        }
        if(curA!=null){
            cur.next = curA;
        }
        if(curB!=null){
            cur.next = curB;
        }
        return dummyHead.next;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
// 读取第一个链表的节点数量
        int n1 = sc.nextInt();
        ListNode head1 = null, tail1 = null;
        for (int i = 0; i < n1; i++) {
            int val = sc.nextInt();
            ListNode newNode = new ListNode(val);
            if (head1 == null) {
                head1 = newNode;
                tail1 = newNode;
            } else {
                tail1.next = newNode;
                tail1 = newNode;
            }
        }

        // 读取第二个链表的节点数量
        int n2 = sc.nextInt();
        ListNode head2 = null, tail2 = null;
        for (int i = 0; i < n2; i++) {
            int val = sc.nextInt();
            ListNode newNode = new ListNode(val);
            if (head2 == null) {
                head2 = newNode;
                tail2 = newNode;
            } else {
                tail2.next = newNode;
                tail2 = newNode;
            }
        }
        ListNode forRes = mergeList(head1,head2);
        while(forRes!=null){
            System.out.print(forRes.val+" ");
            forRes = forRes.next;
        }
    }

}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本题要求合并两个有序链表。对于链表中的每一个节点,由于链表有序的,所以可以将两个链表中的节点按照大小顺序进行比较,然后逐个将较小的节点链接上去,最终得到一个新的有序链表。 使用Rust语言实现这个问题,需要首先定义一个链表节点的结构体,包含node值以及next指针。然后定义一个函数来合并两个有序链表,输入为两个链表的头节点指针,输出为新的有序链表的头节点指针。 在合并过程中,首先需要判断两个链表的头节点哪一个较小,将较小的节点作为新链表的头节点,并将该节点的next指针指向递归调用合并函数的结果。递归结束条件为其中一个链表为空,则将另一个链表直接链接到新链表上。 完整代码如下: ```rust // 定义链表节点结构体 #[derive(Debug)] struct ListNode { val: i32, next: Option<Box<ListNode>>, } impl ListNode { fn new(val: i32) -> Self { ListNode { val, next: None } } } // 合并两个有序链表 fn merge_two_lists(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { match (l1, l2) { (None, None) => None, // 两个链表均为空 (l1, None) => l1, // 其中一个链表为空,直接返回另一个链表 (None, l2) => l2, (Some(mut l1), Some(mut l2)) => { if l1.val < l2.val { l1.next = merge_two_lists(l1.next, Some(l2)); Some(l1) } else { l2.next = merge_two_lists(Some(l1), l2.next); Some(l2) } } } } // 测试代码 fn main() { let l1 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let l2 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 3, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let merged = merge_two_lists(l1, l2); println!("{:?}", merged); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值