MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / mergeTwoLists

Method mergeTwoLists

memo/listnode.cpp:21–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19class Solution {
20public:
21 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
22 ListNode* preHead = new ListNode(-1);
23 ListNode* cur = nullptr;
24 ListNode* pre = preHead;
25 pre->next = cur;
26
27 while(l1 && l2) {
28 if (l1->val <= l2->val) {
29 pre->next = l1;
30 pre = pre->next;
31 l1 = l1->next;
32 } else {
33 pre->next = l2;
34 pre = pre->next;
35 l2 = l2->next;
36 }
37 }
38
39 pre->next = l1 == nullptr ? l2 : l1;
40 return preHead->next;
41 }
42};
43
44

Callers 1

mainFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected