标签:merge 单链表 合并单链表 sorted list
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
思路:要求合并两个排好序的链表。开始我们初始化头front和尾tail,然后从两个单链表的头部比较两个单链表,两链表同时不为空的条件下递归比较:
接着,如果l1不为空,则将尾指针的下一个指向l1,如果l2不为空,则将尾指针的下一个指向l2,然后将front右移一位,
最后返回front即可。
Code(c++):
/**
* 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(l1 == NULL) return l2;
if(l2 == NULL) return l1;
ListNode* front = new ListNode(-1);
ListNode* tail = front;
while( l1 && l2){
if(l1->val < l2->val){
tail->next = l1;
l1 = l1->next;
tail = tail->next;
}else {
tail->next = l2;
l2 = l2->next;
tail = tail->next;
}
}
if(l1){
tail->next = l1;
}
if(l2){
tail->next = l2;
}
front = front->next;
return front;
}
};
Leetcode[21]-Merge Two Sorted Lists
标签:merge 单链表 合并单链表 sorted list
原文地址:http://blog.csdn.net/dream_angel_z/article/details/46440883