排序链表的合并

文章提供了两种解决LeetCode第21题的方法。方法一是使用递归,通过比较两个链表节点的值,将较大的节点放在后面。方法二是创建临时头节点,遍历两个链表,将较大的节点插入到临时链表中。两种方法均实现了将两个有序链表合并成一个有序链表的功能。
摘要由CSDN通过智能技术生成

leetcode21题

0d3f42fa69054b63908a71d7af7a7ddf.png

方法一:使用递归,先将两个链表判空,然后使用递归比较大小并将大的节点放在后面

代码:

include "stdio.h"
#include "stdlib.h"

typedef struct  ListNode{
    int val;
    struct ListNode  *next;
}ListNode;

ListNode*mergeTwoLists(ListNode*list1, ListNode*list2){
    if(list1 == NULL){
        return list2;
    }
    if(list2 == NULL){
        return list1;
    }
    if(list1-> val >= list2-> val){
        list2-> next = mergeTwoLists(list1, list2-> next);
        return list2;
    }
    list1-> next = mergeTwoLists(list1-> next, list2);
    return list1;
}

int main() {
    ListNode a;
    ListNode b;
    ListNode c;
    ListNode d;
    ListNode e;
    ListNode f;
    a.val = 1;
    b.val = 4;
    c.val = 6;
    d.val = 0;
    e.val = 5;
    f.val = 7;
    a.next = &b;
    b.next = &c;
    c.next = null;
    d.next = &e;
    e.next = &f;
    f.next = null;
    ListNode *l1 = &a;
    ListNode *l2 = &d;
    while(l1){
        printf("%d ", l1-> val );
        l1 = l1-> next;        
    }
    printf("\n");
    while(l2){
        printf("%d ", l2-> val );
        l2 = l2-> next;        
    }
    ListNode*list = mergeTwoLists(&a, &d);
    printf("\n" );
    while (list) {
        printf("%d ", list-> val );
        list = list-> next;
    }
    return 0;
}

方法二:使用临时头节点,将大的节点放在前面,小的放在后面(暴力解法 )


#include "stdio.h"
#include "stdlib.h"
#include "math.h"

typedef struct  ListNode{
    int val;
    struct ListNode  *next;
}ListNode;

ListNode*mergeTwo(ListNode*l1, ListNode*l2){
    ListNode temp_head; //设置临时头节点temp_head
    ListNode*pre = &temp_head; //使用pre指针指向temp_head
    while (l1&&l2) {
        if(l1-> val < l2-> val){//l1与l2同时不空时,对他们进行比较
            pre-> next = l1;
            l1 = l1-> next;
        }else{
            pre-> next = l2;
            l2 = l2-> next;
        }
        pre = pre-> next;
    }
    if(l1){
        pre-> next = l1;
    }
    if(l2){
        pre-> next = l2;
    }
    return temp_head.next;
}

int main() {
    ListNode a;
    ListNode b;
    ListNode c;
    ListNode d;
    ListNode e;
    ListNode f;
    a.val = 1;
    b.val = 4;
    c.val = 6;
    d.val = 0;
    e.val = 5;
    f.val = 7;
    a.next = &b;
    b.next = &c;
    c.next = null;
    d.next = &e;
    e.next = &f;
    f.next = null;
    ListNode *l1 = &a;
    ListNode *l2 = &d;
    while(l1){
        printf("%d ", l1-> val );
        l1 = l1-> next;        
    }
    printf("\n");
    while(l2){
        printf("%d ", l2-> val );
        l2 = l2-> next;        
    }
    ListNode*list = mergeTwo(&a, &d);
    printf("\n" );
    while (list) {
        printf("%d ", list-> val );
        list = list-> next;
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值