21. 合并两个有序链表 - 力扣(LeetCode)( 点击前面链接即可查看题目)
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{struct ListNode* guard = (struct ListNode*)malloc(sizeof(struct ListNode));struct ListNode* cur = guard;if(NULL == list1)return list2;if(NULL == list2)return list1;while(list1 && list2){if(list1->val < list2->val){cur->next = list1;list1 = list1->next;cur = cur->next;cur->next == NULL;}else{cur->next = list2;list2 = list2->next;cur = cur->next;cur->next == NULL;}}if(NULL == list1){cur->next = list2;}else if(NULL == list2){cur->next = list1;}cur = guard->next;free(guard);return cur;
}