剑指offer-面试题17.合并两个排序的链表

题目:输入两个递增的排序的链表,合并这两个链表并使新链表中的节点仍然是

按照递增排序的。例如链表1链表2合并为链表3.

1 List1:1->3->5->7
2
3 List2:2->4->6->8
4 
5 List3:1->2->3->4->5->6->7->8

链表结点定义如下:

1 struct ListNode
2 {
3     int     m_nValue;
4     ListNode* m_pNext;
5 }

 

其实我们可以简单梳理下流程如下:

1.两个指针分别指向List1和List2的头结点。设为ptr1和ptr2

2.比较ptr1和ptr2结点的值,ptr1<ptr2则ptr1则是合并后的链表头结点

3.ptr1向后移动一个结点此时再比较 ptr1>ptr2则将ptr2的节点插入到头结点之后

4.当ptr1或者ptr2到达末尾时 比如ptr1到达List1结尾后 若此时ptr2还未到List2结尾

   将ptr2插入到新排序的链表后面.

 

代码实现如下:

  1 #include <iostream>
  2 using namespace std;
  3 
  4 struct ListNode
  5 {
  6     int data;
  7     struct ListNode *next;
  8 };
  9 
 10 struct ListNode* CreateList()
 11 {
 12     struct ListNode* Head,*p;
 13     Head=(struct ListNode*)malloc(sizeof(ListNode));
 14     Head->data=0;
 15     Head->next=NULL;
 16     p=Head;
 17     
 18     cout<<"Create List....(0-exit!)"<<endl;
 19     while(true)
 20     {
 21         int Data;
 22         cin>>Data;
 23         if(Data!=0)
 24         {
 25             struct ListNode* NewNode;
 26             NewNode=(struct ListNode*)malloc(sizeof(ListNode));
 27             NewNode->data=Data;
 28             NewNode->next=NULL;
 29             p->next=NewNode;
 30             p=p->next;
 31         }
 32         else
 33         {
 34             break;
 35         }
 36     }
 37     
 38     return Head->next;
 39 }
 40 
 41 void PrintList(struct ListNode* Head)
 42 {
 43     cout<<"The List is: ";
 44     
 45     struct ListNode *p;
 46     p=Head;
 47     while(p!=NULL)
 48     {
 49         cout<<p->data<<" ";
 50         p=p->next;
 51     }
 52     cout<<endl;
 53 }
 54 
 55 struct ListNode* Merge(ListNode* pHead1,ListNode* pHead2)
 56 {
 57     if(pHead1==NULL&&pHead2==NULL)
 58         return NULL;
 59     
 60     if(pHead1==NULL&&pHead2!=NULL)
 61         return pHead2;
 62 
 63     if(pHead1!=NULL&&pHead2==NULL)
 64         return pHead1;
 65 
 66     struct ListNode *ptr1,*ptr2,*MergeList,*newhead;;
 67     
 68     ptr1=pHead1;
 69     ptr2=pHead2;
 70 
 71     if(ptr1->data>ptr2->data)
 72     {
 73         MergeList=ptr2;
 74         ptr2=ptr2->next;
 75     }
 76     else
 77     {
 78         MergeList=ptr1;
 79         ptr1=ptr1->next;
 80     }
 81 
 82     newhead=MergeList;
 83 
 84     while(ptr1!=NULL&&ptr2!=NULL)
 85     {
 86         if(ptr1->data>ptr2->data)
 87         {
 88             MergeList->next=ptr2;
 89             ptr2=ptr2->next;
 90             MergeList=MergeList->next;
 91         }
 92         
 93         if(ptr1->data<ptr2->data)
 94         {
 95             MergeList->next=ptr1;
 96             ptr1=ptr1->next;
 97             MergeList=MergeList->next;
 98         }
 99     }
100 
101     if(ptr1!=NULL)
102     {
103         while(ptr1!=NULL)
104         {
105             MergeList->next=ptr1;
106             ptr1=ptr1->next;
107             MergeList=MergeList->next;
108         }
109         MergeList->next=NULL;
110     }
111     if(ptr2!=NULL)
112     {
113         while(ptr2!=NULL)
114         {
115             MergeList->next=ptr2;
116             ptr2=ptr2->next;
117             MergeList=MergeList->next;
118         }
119         MergeList->next=NULL;
120     }
121 
122 
123     return newhead;
124 }
125 
126 int main()
127 {
128     ListNode *List1,*List2,*MergeList;
129     List1=CreateList();
130     PrintList(List1);
131     List2=CreateList();
132     PrintList(List2);
133     MergeList=Merge(List1,List2);
134     PrintList(MergeList);
135     return 0;
136 }

运行截图:

posted @ 2015-07-23 22:26  vpoet  阅读(194)  评论(0编辑  收藏  举报