zoukankan      html  css  js  c++  java
  • 两个带有头结点递增链表合并成一个递减链表 而且不能借助其他空间

    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);
    }
    
    
  • 相关阅读:
    Stars in Your Window POJ
    Adding New Machine ZOJ
    洛谷 P3400 仓鼠窝
    django启动时报错:Apps aren't loaded yet.
    netstat命令简单使用
    iostat命令简单使用
    linux下查找指定时间内修改过的或新建的文件
    nginx学习之压缩解压篇(七)
    nginx学习之反向代理篇(六)
    nginx学习之静态内容篇(五)
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811923.html
Copyright © 2011-2022 走看看