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;
* ListNode *next;
* ListNode (int x): Val (x), Next (NULL) {}
* };
*/
Class Solution {
Public
listnode* mergetwolists (listnode* L1, listnode* L2) {
/* If one is empty and then return another */
if (L1 = = NULL) {
return L2;
}
if (L2 = = NULL) {
return L1;
}
ListNode *t1 = L1;
ListNode *t2 = L2;
ListNode *tr, *result;
tr = NULL;
result = NULL;
while (T1 | | t2) {
if (t1 = = NULL) {
Tr->next = T2;
Break
}
if (t2 = = NULL) {
Tr->next = T1;
Break
}
ListNode *tt;
if (T1->val < T2->val) {
tt = T1;
T1 = t1->next;
} else {
tt = T2;
T2 = t2->next;
}
if (!result) {//The Fisrst time to add a Nodel
TR = TT;
result = TT;
} else {
Tr->next = TT;
TR = TT;
}
}
return result;
}
};
[Leetcode] 21-merge, Sorted Lists