两个带有头结点递增链表合并成一个递减链表 而且不能借助其他空间

List Merge(List L1, List L2)
{	
    List L = (List)malloc(sizeof(struct Node));
	List p3 = L;
	List p1 = L1->Next, p2 = L2->Next;
	while (p1 != NULL && p2 != NULL)
	{
		if (p1->Data < p2->Data)
		{
			p3->Next = p1;
			p3 = p1;
			p1 = p1->Next;
		}
		else
		{
			p3->Next = p2;
			p3 = p2;
			p2 = p2->Next;
		}
	}
	p3->Next = p1 ? p1 : p2;
	L1->Next = NULL;
	L2->Next = NULL;
	return L;
}   
#include<iostream>
using namespace std;
#include<vector>
struct node
{
	int data;
	node* next;
};
typedef node* list;
list reverse(list a)
{
	list pre = NULL, cur = a->next;
	list temp;
	while (cur != NULL)
	{
		temp = cur->next;
		cur->next = pre;
		pre = cur;
		cur = temp;
	}
	return pre;
}
list init()
{
	list a = new node;
	a->next = NULL;
	return a;
}
list insert(list a,int i,int x)
{
	list p = a;
	for (int k = 0; k < i; k++)
	{
		if (p == NULL) break;
		else p = p->next;
	}
	list newNode = new node;
	newNode->data = x;
	newNode->next = p->next;
	p->next = newNode;
	return a;
}
void printf(list a)
{
	while (a->next != NULL)
	{
		a = a->next;
		cout << a->data << " ";
	}
	cout << endl;
}
int main()
{
	list a = init(), b = init();
	for (int i = 0; i < 4; i++)
	{
		a = insert(a,i,i);
	}
	a = reverse(a);
	list temp = new node;
	temp->next = a;
	a = temp;
	printf(a);
	int q = 0;
	for (int i = -3; i < 4; i+=2)
	{

		 b= insert(b,q,i);
		 q++;
	}
	b = reverse(b);
	list temp1 = new node;
	temp1->next = b;
	b = temp1;
	printf(b);
	list pb = b;
	list pa = a->next;
	while (pb->next != NULL)
	{
		pa = a->next;
		int i = 0;
		pb = pb->next;
		while (pa!=NULL&&pb->data <= pa->data)
		{
			pa = pa->next;
			i++;
		}
		a=insert(a, i, pb->data);
	}
	printf(a);
}

原文地址:https://www.cnblogs.com/Hsiung123/p/13811923.html