首页 > 代码库 > Merge Two Sorted Lists

Merge Two Sorted Lists

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {    struct ListNode sentinel;    struct ListNode *sortedTail = &sentinel;    while (l1 != NULL && l2 != NULL) {        if (l1->val < l2->val) {            sortedTail->next = l1;            sortedTail = l1;            l1 = l1->next;        } else {            sortedTail->next = l2;            sortedTail = l2;            l2 = l2->next;        }    }        if (l1 != NULL) {        sortedTail->next = l1;    } else {        sortedTail->next = l2;    }    return sentinel.next;}

大退步。。用了20分钟T_T

                                                l1

                /

 sortedHead->...->sortedTail

                \

                                                l2

Merge Two Sorted Lists