Title Link: https://leetcode.com/problems/merge-two-sorted-lists/
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.
/** * Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode *next; *}; */struct listnode* mergetwolists (struct listnode* L1, struct listnode* L2) { struct listnode* head; struct listnode* p; 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; } p = head; while (L1 && L2) { if (L1->val < L2->val) { p->next = L1; L1 = l1->next; p = p->next; } else { p->next = L2; L2 = l2->next; p = p->next; } } if (L1) p->next = L1; if (L2) p->next = L2; return head;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
#21 Merge Sorted Lists