LeetCode开心刷题第十一天——21. Merge Two Sorted Lists(operate not familiar)

21. Merge Two Sorted Lists
Easy

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

Although this is a easy question,I'm not familiar with nodelist operator.
This code clear and nest.Perfect thing is that it add a node in front of the List,So many NULL
situation we needn't consider.So in the end we only need to return first->next,It's what we need
But this use to new ListNode each time it need toevaluation,It's not solve the input problem by
the root.As I have mentioned before,it does not embody the idea of programming from special to
general!
#include "test1.h"
#include <iostream>  
using namespace std;  
  
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
 
class Solution{
 
public:
    //LeetCode中的方法名称  
    ListNode* mergeTwoLists(ListNode* l1,ListNode* l2) {
    ListNode* head = new ListNode(0);//head指针是每次后移的  
    ListNode* firstNode = head;//需要确定好一个不变的头结点,然后返回这个头结点的next地址,才能返回完成的链表  
  
    while(l1!=NULL && l2!=NULL){  
        if(l1->val > l2->val){  
            head->next = l2;//这里只是确定好head->next是哪个结点,head还没有指向这个结点  
            l2 = l2->next;  
        }else{  
            head->next = l1;  
            l1 = l1->next;  
        }  
        head = head ->next;//head指向这个结点  
    }//while  
  
    head->next = l1?l1:l2;//遇到一个为空后,哪个不为空就next指向哪一个  
    //return head->next;//输出是:7 8 ,因为head指针也在往后移动  
    return firstNode->next;  
   }
};
ListNode* initList1(ListNode* head){//初始化第一个链表  
    ListNode* node1  = new ListNode(2);  
    head->next = node1;  
  
    ListNode* node2  = new ListNode(3);  
    node1->next = node2;  
  
    ListNode* node3  = new ListNode(6);  
    node2->next = node3;  
  
    ListNode* node4  = new ListNode(8);  
    node3->next = node4;  
    return head;  
}//initList1  
  
ListNode* initList2(ListNode* head){//初始化第二个链表  
    ListNode* node1  = new ListNode(1);  
    head->next = node1;  
  
    ListNode* node2  = new ListNode(2);  
    node1->next = node2;  
  
    ListNode* node3  = new ListNode(4);  
    node2->next = node3;  
  
    ListNode* node4  = new ListNode(5);  
    node3->next = node4;  
    return head;  
}//initList2  
  
//从头至尾遍历单链表  
void visitList(ListNode* head){  
    while(head!= NULL){  
        cout<<head->val<<endl;  
        head= head->next;  
    }  
}//visitList  
  
int main()    
{    
    //函数中初始化  
     cout<<"第一个链表:"<<endl;  
     ListNode* head1  = new ListNode(0);
     head1 = initList1(head1);  
     visitList(head1);  
  
     cout<<"第二个链表:"<<endl;  
     ListNode* head2  = new ListNode(-1);  
     head2 = initList2(head2);  
     visitList(head2);  
 
     cout<<"合并后的新链表:"<<endl;  
     Solution solution;//正确
     ListNode* head = solution.mergeTwoLists(head1,head2);//调用成员方法
     visitList(head);
  
     return 0;    
}
 
//第一个链表:
//0
//2
//3
//6
//8
//第二个链表:
//-1
//1
//2
//4
//5
//合并后的新链表:
//-1
//0
//1
//2
//2
//3
//4
//5
//6
//8
//请按任意键继续. . .
 
 

My solve:

#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<vector>
#include<list>
#include<queue>
#include<algorithm>
#include<stack>
#include<map>
using namespace std;


  struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
  };

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* head=NULL;
        ListNode* next=NULL;
        if(l1==NULL)
            return l2;
        if(l2==NULL)
            return l1;
        if(l1->val<=l2->val)
        {
            head=l1;
            l1=l1->next;
        }
        else
        {
            head=l2;
            l2=l2->next;
        }
        next=head;
        while(true)
        {
            if(l1==NULL)
            {
                next->next=l2;  
                return head; 
            }
            if(l2==NULL)
            {
                next->next=l1;
                return head;
            }
            if(l1->val<=l2->val)
            {
                next->next=l1;
                next=next->next;
                l1=l1->next;
            }
            else
            {
                next->next=l2;
                next=next->next;
                l2=l2->next;
                
            }
        }
        return head;
    }
};
int main()
{
    ListNode *p,*head,*l,*head2,*l2;
    int n;
    p=(ListNode*)malloc(sizeof(ListNode*));
    head=p;
    head2=l2;
    p->val=0;
    p->next=NULL;
    char ch1;
    while((ch1=cin.get())!='\n' )
    {
        cin.putback(ch1);
        scanf("%d",&p->val);
        l=(ListNode*)malloc(sizeof(ListNode*));
        l->val=0;
        l->next=NULL;
        p->next=l;
        p=p->next;
    }
    while((ch1=cin.get())!='\n' )
    {
        cin.putback(ch1);
        scanf("%d",&p->val);
        l=(ListNode*)malloc(sizeof(ListNode*));
        l->val=0;
        l->next=NULL;
        p->next=l;
        p=p->next;
    }
    return 0;
}

In this way,you can use input to evaluate ListNode

 

猜你喜欢

转载自www.cnblogs.com/Marigolci/p/11088949.html