单链表合并

题目:设ha和hb分别是两个带附加头结点的非递减有序单链表的头指针。将两个表合并成一个非递增有序单链表。结果不能占用除两个链表外多余的存储空间。

我的思路:先合并成非递减有序,然后再翻转链表,

不知道有没有比这更好的办法,先写在这

代码:

main.c

#include"list.h"

int main(void)
{ 
     node * ha = list_init();
     node * hb = list_init();
     int arr1[]= {1,3,4,4,9,10};
     int arr2[]= {2,3,4,13,13,14};
     // 14 13 13 10 9 4 4 4 3 2 1
     int n = sizeof(arr1) / sizeof(*arr1);
     int m = sizeof(arr2) / sizeof(*arr2);

     list_create(ha, arr1, n);
     list_show(ha);

     list_create(hb, arr2, m);
     list_show(hb);

     merge(ha,hb);    //排序
     list_show(ha);

     turn(ha);       //翻转
     list_show(ha);

     return 0;
}

list.c

#include "list.h"

node *list_init()
{
     node *ptr = malloc(sizeof(node));
     if (ptr == NULL)
          return NULL;
     ptr->next = NULL;
     return ptr;
}
void list_create(node *ptr, int *a, int n)
{
     node *p = ptr;
     for (int i = 0; i < n; i++)
     {
          node *new = malloc(sizeof(node));
          if (new == NULL)
               exit(1);
          new->next = NULL;
          new->data = a[i];
          p->next = new;
          p = new;
     }
}
void list_show(node *ptr)
{
     node *p = ptr;
     while (p->next != NULL)
     {
          p = p->next;
          printf("%d  ", p->data);
     }
     printf("\n");
}

void merge(node *ha,node *hb)
{
     node *pa = ha->next;
     node *pb = hb->next;
     node *p = ha, *q = NULL;
     while (pa != NULL && pb != NULL)
     {
          if (pa->data > pb->data)
          {
               q = pb;
               pb = pb->next;
               p->next = q;
               q->next = pa;
               p = p->next;
          }
          else
          {
               pa = pa->next;
               p = p->next;
          }
     }
     if(pa == NULL)
          p->next = pb;
}
void turn(node *ha)
{
     node *pa = ha->next;
     node *tail = NULL;
     node *current = ha;

     while (current->next != NULL)
          current = current->next;

     tail = current;

     node * head = pa;

     while(head != tail)
     {
          current = head;
          head = head->next;
          current->next = tail->next;
          tail->next = current;
     }
     ha->next = tail;
}

list.h

#ifndef __list_H__
#define __list_H__

#include<stdio.h>
#include<stdlib.h>

typedef struct node_st
{
	int data;
	struct node_st * next;
}node;

node *list_init();
void list_show(node *ptr);
void list_create(node *ptr, int *a, int n);
void merge(node *ha,node *hb);
void turn(node *ha);

#endif
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将两个单链表合并成一个单链表的C++代码: ```c++ #include <iostream> using namespace std; // 定义单链表结构体 struct Node { int val; Node* next; Node(int x) : val(x), next(NULL) {} }; // 将两个单链表合并成一个单链表 Node* merge_list(Node* l1, Node* l2) { Node* head = new Node(-1); // 新链表的头结点 Node* p = head; // 新链表的当前节点 while (l1 != NULL && l2 != NULL) { if (l1->val < l2->val) { p->next = l1; l1 = l1->next; } else { p->next = l2; l2 = l2->next; } p = p->next; } if (l1 != NULL) { p->next = l1; } if (l2 != NULL) { p->next = l2; } return head->next; } // 输出单链表 void print_list(Node* head) { Node* p = head; while (p != NULL) { cout << p->val << " "; p = p->next; } } int main() { Node* l1 = new Node(1); l1->next = new Node(3); l1->next->next = new Node(5); Node* l2 = new Node(2); l2->next = new Node(4); l2->next->next = new Node(6); cout << "合并前的链表1:" << endl; print_list(l1); cout << endl; cout << "合并前的链表2:" << endl; print_list(l2); cout << endl; Node* merged_list = merge_list(l1, l2); cout << "合并后的链表:" << endl; print_list(merged_list); cout << endl; return 0; } ``` 在 `merge_list()` 函数中,首先创建一个新链表的头结点,并用 `p` 指向该节点。然后比较两个链表 `l1` 和 `l2` 中当前节点的值,将较小的节点加入新链表,并将指针移动到下一个节点。直到其中一个链表为空,将剩余的节点加入新链表中。最后返回新链表的头节点。 在 `main()` 函数中,先创建两个单链表 `l1` 和 `l2`,输出合并前的两个链表,调用 `merge_list()` 函数将两个链表合并成一个链表,最后输出合并后的链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值