Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.
A list of two well-arranged lists, merged into a new set of linked lists
1, their own way of simplicity
/** Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode *next; *};*/structlistnode* mergetwolists (structlistnode* L1,structlistnode*L2) { structlistnode* tmp =malloc(sizeof(structListNode)); structlistnode* head =tmp; structlistnode*NEXTP; while(L1! = NULL && L2! =NULL) {NEXTP=malloc(sizeof(structListNode)); if(L1->val < l2->val) {NEXTP->val = l1->Val; NEXTP->next =NULL; TMP->next =NEXTP; TMP=NEXTP; L1= l1->Next; Continue;
//There is a problem here because no elseif is used, so if you don't use continue, you will continue to make a mistake. } if(L1->val > l2->val) {NEXTP->val = l2->Val; NEXTP->next =NULL; TMP->next =NEXTP; TMP=NEXTP; L2= l2->Next; Continue; } if(L1->val = = l2->val) { structListNode *twice =malloc(sizeof(structListNode)); Twice->val = l1->Val; Twice->next =NEXTP; NEXTP->val = l1->Val; NEXTP->next =NULL; TMP->next =twice; TMP=NEXTP; L1= l1->Next; L2= l2->Next; Continue; } } if(L1 = =NULL) {tmp->next =L2; returnHead->Next; } if(L2 = =NULL) {tmp->next =L1; returnHead->Next; } returnHead->next;}
- Someone else's algorithm is in the middle that piece does not need to be specifically judged at the time of two values equal
Merge Sorted Lists