LeetCode - 有序链表问题类总结

合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。不要新建节点,减少内存占用。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

/**
 * 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 == nullptr)
			return l2;
		if (l2 == nullptr)
			return l1;
		ListNode* res = nullptr;
		if (l1->val > l2->val)
			res = l2;
		else
			res = l1;
		ListNode* pre = nullptr;
		while (l1!=nullptr&&l2!= nullptr) {
			if (l1->val <= l2->val)
			{
				if (pre != nullptr) {
					pre->next = l1;
				}
				pre = l1;
				l1 = l1->next;
			}
			else {
				if (pre != nullptr) {
					pre->next = l2;
				}
				pre = l2;
				l2 = l2->next;
			}
		}
		while (l1 != nullptr) {
			pre->next = l1;
			pre = l1;
			l1 = l1->next;
		}
		while (l2 != nullptr) {
			pre->next = l2;
			pre = l2;
			l2 = l2->next;
		}
		return res;
	}
};

合并K个排序链表

合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
示例:
输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6

分析:可采用分治法,合并k个转换为合并两个有序链表进行分析

//分治法
/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
   ListNode* mergeKLists(vector<ListNode*>& lists) {
       if(lists.empty()){
           return NULL;
       }
       if(lists.size()==1){
           return lists[0];
       }
       if(lists.size()==2){
           return mergeTwoLists(lists[0],lists[1]);
       }
       int mid = lists.size()/2;
       vector<ListNode*> sub_list1;
       vector<ListNode*> sub_list2;
       for(int i = 0;i<mid;i++){
           sub_list1.push_back(lists[i]);
       }
       for(int i = mid;i<lists.size();i++){
           sub_list2.push_back(lists[i]);
       }
        ListNode* l1 = mergeKLists(sub_list1);
        ListNode* l2 = mergeKLists(sub_list2);
       return  mergeTwoLists(l1,l2);
   }
   
 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
   	if (l1 == nullptr)
   		return l2;
   	if (l2 == nullptr)
   		return l1;
   	ListNode* res = nullptr;
   	if (l1->val > l2->val)
   		res = l2;
   	else
   		res = l1;
   	ListNode* pre = nullptr;
   	while (l1!=nullptr&&l2!= nullptr) {
   		if (l1->val <= l2->val)
   		{
   			if (pre != nullptr) {
   				pre->next = l1;
   			}
   			pre = l1;
   			l1 = l1->next;
   		}
   		else {
   			if (pre != nullptr) {
   				pre->next = l2;
   			}
   			pre = l2;
   			l2 = l2->next;
   		}
   	}
   	while (l1 != nullptr) {
   		pre->next = l1;
   		pre = l1;
   		l1 = l1->next;
   	}
   	while (l2 != nullptr) {
   		pre->next = l2;
   		pre = l2;
   		l2 = l2->next;
   	}
   	return res;
   }
};
//解法二
/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
   public:
   	ListNode* mergeKLists(vector<ListNode*>& lists) {
   		if (lists.size() == 0)
   			return nullptr;
   		ListNode* res = check(lists);
   		ListNode* pre = res;
   		ListNode* nextptr = nullptr;
   		while (nextptr = check(lists)) {
   			pre->next = nextptr;
   			pre = nextptr;
   		}
   		return res;
   	}

   	ListNode* check(vector<ListNode*>& lists) {
   		//删除指针移动nullptr的链表,并选出已存在的连标中较小的节点
   		vector<ListNode*>::iterator minit = lists.begin();
   		for (auto it = lists.begin(); it != lists.end(); )
   		{
   			if (*it == nullptr)
   				it = lists.erase(it);
   			else
   			{
   				if (*minit == nullptr || ((*minit)->val > (*it)->val))
   					minit = it;
   				it++;
   			}
   		}
   		if (*minit == nullptr)
   			return nullptr;
   		ListNode* res = *minit;
   		(*minit) = (*minit)->next;
   		return res;
   	}
   };
发布了76 篇原创文章 · 获赞 6 · 访问量 2773

猜你喜欢

转载自blog.csdn.net/u014618114/article/details/104176943