zoukankan      html  css  js  c++  java
  • 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

    // test14.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<cctype>
    #include <vector>
    #include<exception>
    #include <initializer_list>
    #include<stack>
    #include <algorithm>
    
    using namespace std;
    
    struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
    val(x), next(NULL) {
    }
    };
    class Solution {
    public:
    	ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    	{
    		ListNode *newHead = pHead1, *temp;
    		vector<int>vec;
    		if (pHead1 == NULL)
    			return pHead2;
    		if (pHead2 == NULL)
    			return pHead1;
    		
    		while (pHead1->next!=NULL)
    		{
    			pHead1 = pHead1->next;
    		}
    
    		pHead1->next = pHead2;
    		temp = newHead;
    
    		while (newHead!=NULL)
    		{
    			vec.push_back(newHead->val);
    			newHead = newHead->next;
    		}
    		
    		newHead = temp;
    		sort(vec.begin(),vec.end());
    
    		for (auto it = vec.begin(); it != vec.end(); it++)
    		{
    			temp->val = *it;
    			temp = temp->next;
    		}
    		
    		return newHead;
    
    	}
    };
    
    int main()
    {
    	vector<int> vec;
    	Solution so;
    
    	
    	ListNode first(1);
    	ListNode second(4);
    	ListNode third(5);
    	ListNode four(6);
    	ListNode *head=&first;
    	first.next = &second;
    	second.next = &third;
    	third.next = &four;
    
    
    	ListNode first1(1);
    	ListNode second1(2);
    	ListNode third1(3);
    	ListNode four1(7);
    	ListNode *head1 = &first1;
    	first1.next = &second1;
    	second1.next = &third1;
    	third1.next = &four1;
    
    
    
    	
    	ListNode *result = so.Merge(head, head1);
    	while (result !=NULL)
    	{
    		cout << (*result).val<<"  ";
    		result = (*result).next;
    	}
    
    	cout << endl;
    	
    	return 0;
    }
  • 相关阅读:
    大爽Python入门教程 0-2 什么是IDE?python选什么IDE好?
    大爽Python入门教程 0-3 安装Atom与配置Python环境
    大爽Python入门教程 0-1 安装python
    HBase 过滤器 及shell 操作过滤器
    分布式ID生成方式(雪花算法)
    tidb 踩坑经验(转载)
    MYSQL ACID
    kafka什么时候会丢消息
    java 单例模式模式实现
    使用 GitHub 的十个最佳实践
  • 原文地址:https://www.cnblogs.com/wdan2016/p/5960083.html
Copyright © 2011-2022 走看看