Topic 1519: Merging two sorted lists
-
Title Description:
-
Input two monotonically increasing list, output two list of linked lists, of course, we need to synthesize the linked list to meet the monotone non-reduction rules.
(Hint: Be sure to use a linked list.) )
-
Input:
-
The input may contain multiple test samples, and the input ends with EOF.
For each test case, enter the first behavior of two integers n and m (0<=n<=1000, 0<=m<=1000): N represents the number of elements of the first linked list that will be entered, and M represents the number of elements of the second linked list to be entered.
The following line includes n number t (1<=t<=1000000): Represents the element in the chain table one. The next line contains m elements, s (1<=t<=1000000).
-
Output:
-
corresponding to each test case,
If there is a result, output the corresponding linked list. Otherwise, the output is null.
-
Sample input:
-
5 21 3 5 7 92 40 0
-
-
Sample output:
-
1 2 3 4 5 7 9NULL
Idea: Since both of these lists are already lined up, all two lists are traversed from the back, when the small one is added to the back of the new table, and then the pointer is moved backwards
This question mainly to consider the special case, whether the two linked list is empty, and whether it has reached the end of a list of pieces to judge.
1#include <iostream>2#include <vector>3 using namespacestd;4 5 //data structure of a linked list6 structListNode7 {8 intVal;9listnode*Next;TenListNode (intx): Val (x), Next (NULL) {} One }; A - //Create a single linked list -listnode* Product (vector<int>& VEC,intN) the { - if(n==0) - returnNULL; -listnode* ptr1=NewListNode (vec[0]); + intI=1; -listnode* ptr2=ptr1; + while(i<N) A { atptr2->next=NewListNode (Vec[i]); -Ptr2=ptr2->Next; -i++; - } - returnptr1; - } in - //output of each node of the single-linked list to voidCout_node (listnode*root) + { - if(root==NULL) the return; *listnode* ptr1=Root; $ while(ptr1!=NULL)Panax Notoginseng { -cout<<ptr1->val<<' '; thePtr1=ptr1->Next; + } Acout<<Endl; the return; + } - $listnode* Merge (listnode* root1,listnode*Root2) $ { -listnode* root=NULL; - if(Root1==null&&root2==null)//Two linked lists are empty the returnRoot; - if(Root1==null)//Root2 is emptyWuyi returnRoot2; the if(Root2==null)//root1 is empty - returnroot1; Wulistnode* ptr1=root1; -listnode* ptr2=Root2; About $ //in determining that two linked lists are not empty, one of the smaller nodes is given root . - if(root1->val<root2->val) - { -root=root1; APtr1=ptr1->Next; + } the if(root2->val<root1->val) - { $root=Root2; thePtr2=ptr2->Next; the } the if(root1->val==root2->val) the { -root=root1; inPtr1=ptr1->Next; thePtr2=ptr2->Next; the } Aboutlistnode* ptr0=Root; theptr0->next=NULL; the the //Start Loop + while(1) - { the //if one of the linked lists has reached the end ,Bayi if(ptr1==NULL) the { theptr0->next=ptr2; - returnRoot; - } the if(ptr2==NULL) the { theptr0->next=ptr1; the returnRoot; - } the //Otherwise, give the small one to the root list and move the pointer backwards . the if(ptr1->val<ptr2->val) the {94ptr0->next=ptr1; thePtr1=ptr1->Next; thePtr0=ptr0->Next; theptr0->next=NULL;98 } About Else if(ptr1->val>ptr2->val) - {101ptr0->next=ptr2;102Ptr2=ptr2->Next;103Ptr0=ptr0->Next;104ptr0->next=NULL; the }106 Else107 {108ptr0->next=ptr1;109Ptr1=ptr1->Next; thePtr2=ptr2->Next;111Ptr0=ptr0->Next; theptr0->next=NULL;113 } the } the returnRoot; the }117 118 intMain ()119 { - intary1[Ten]={1,3,5,7,9};121vector<int> Vec1 (ary1,ary1+5);122listnode* root1=product (VEC1,5);123 Cout_node (ROOT1);124 the intary2[Ten]={2,4};126vector<int> vec2 (ary2,ary2+2);127listnode* root2=product (VEC2,2); - Cout_node (ROOT2);129 thelistnode* root=merge (Root1,root2);131 Cout_node (root); the 133System"Pause");134}
Sword Point offer surface question 17--merge two sorted list