1.题目:

2.代码:
因为输入的两个链表是递增的,要求输出递减的,所以先归并再就地逆置
#include<iostream>
#include<stdlib.h>
//先进行归并,再进行就地逆置
using namespace std;
typedef struct node{
int value;
struct node *next;
}Node;
Node* PolyCreate(int elenum)
{
Node *head,*rear,*s;
head = (node*)malloc(sizeof(node));
rear = head;
for(int i = 0; i < elenum; i++)
{
int value;
cin >> value;
s = (node*)malloc(sizeof(node));
s->value = value;
rear->next = s;
rear = s;
}
rear->next = NULL;
return head;
}
Node* PolyMerge(Node *pa, Node *pb)
//线性表合并
{
Node *p,*q,*rear;
p = pa->next;
q = pb->next;
rear = pa; //最终归并的序列使用pa的空间,rear指向最终序列的尾端
rear->next = NULL;
while(p != NULL && q != NULL)
{
if(p->value <= q->value)
{
rear->next = p;
rear = p;
p = p->next;
}
else

这篇博客探讨了如何处理两个递增排序的链表,通过归并操作得到一个递减排序的链表。内容包括具体题目、实现代码以及操作中需要注意的细节,如在构建链表时确保尾部指针设为NULL,以防止遍历时产生段错误。
最低0.47元/天 解锁文章
2113

被折叠的 条评论
为什么被折叠?



