Title: Input two monotonically increasing list, output two linked list of the linked list, of course, we need to synthesize the linked list to meet the monotone non-reduction rules.
Idea: Sweep the maintenance increment again, and finally add the parts of the original linked list that may not be added
PublicListNode Merge (listnode list1,listnode list2) {if(list1==NULL&&list2==NULL)return NULL; if(list1==NULL&&list2!=NULL)returnList2; if(list1!=NULL&&list2==NULL)returnList1; ListNode res1=NewListNode (-1); ListNode Res=res1; //to have two pointers pointing to the created head node, a pointer will go to the end and the return time is no longer available. while(list1!=NULL&&list2!=NULL){ if(list1.val<=list2.val) {Res.next=List1; Res=Res.next; List1=List1.next; }Else{Res.next=List2; Res=Res.next; List2=List2.next; } } while(List1! =NULL) {Res.next=List1; Res=Res.next; List1=List1.next; } while(List2! =NULL) {Res.next=List2; Res=Res.next; List2=List2.next; } returnRes1.next; }
Merge two sorted lists