首页
IT
登录
6mi
u
盘
搜
搜 索
IT
21. Merge Two Sorted Lists
21. Merge Two Sorted Lists
xiaoxiao
2021-03-25
92
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode*L = NULL,*q=NULL; if (l1 == NULL&&l2 == NULL) return NULL; if (l1 == NULL&&l2 != NULL) return l2; if (l1 != NULL&&l2 == NULL) return l1; if (l1->val <= l2->val) { L = l1; l1 = l1->next; } else { L = l2; l2 = l2->next; } q = L; while (l1 != NULL&&l2 != NULL) { if (l1->val <= l2->val) { q->next = l1; l1 = l1->next; } else { q->next = l2; l2 = l2->next; } q = q->next; } if (l1 != NULL) q->next = l1; if (l2 != NULL) q->next = l2; return L; } };
转载请注明原文地址: https://ju.6miu.com/read-12824.html
技术
最新回复
(
0
)