链表归并排序与翻转

我在链表排序和翻转上面总是踩坑,在此记录一下。

链表归并排序:

#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <map>

using namespace std;
struct ListNode
{
	ListNode* next;
	int val;
	ListNode(int _val = 0){ 
		val = _val;
		next = NULL;
	}
};

class Solution {
public:
	ListNode* sortList(ListNode* head) {
		if (head == NULL || head->next == NULL) {
			return head;
		}
		return mergeSort(head);//去掉链表尾端的寻找
	}
	ListNode* mergeSort(ListNode* head) {
		if (head == NULL || head->next == NULL){
			return head;
		}
		ListNode *fast = head->next;
		ListNode *slow = head;
		//快慢链表法找中点
		while (fast != NULL&&fast->next != NULL){
			fast = fast->next;                    
			slow = slow->next;
			if (fast != NULL&&fast->next != NULL){        
				fast = fast->next;
			}
		}

		//归并排序
		ListNode* rightList = mergeSort(slow->next);
		slow->next = NULL;
		ListNode* leftList = mergeSort(head);

        //pHead指向队首,pEnd暂时指向队尾
		ListNode *pHead = NULL;
		ListNode *pEnd = NULL;
		if (rightList == NULL){
			return leftList;
		}
		if (rightList->val>leftList->val){
			pEnd = pHead = leftList;
			leftList = leftList->next;
		}
		else{
			pEnd = pHead = rightList;
			rightList = rightList->next;
		}
		//两个链表合并
		while (rightList&&leftList){
			if (rightList->val<leftList->val){
				pEnd->next = rightList;
				rightList = rightList->next;
			}
			else{
				pEnd->next = leftList;
				leftList = leftList->next;
			}
			pEnd = pEnd->next;
		}
		if (rightList){
			pEnd->next = rightList;
		}
		else if (leftList){
			pEnd->next = leftList;
		}
		return pHead;
	}
}; 
int main() {

	Solution sol = Solution();
	ListNode *l1 = new ListNode(7);
	ListNode *l2 = new ListNode(5);
	ListNode *l3 = new ListNode(6);
	ListNode *l4 = new ListNode(2);
	l1->next = l2;
	l2->next = l3;
	l3->next = l4;

	ListNode* newNode = sol.sortList(l1);

	while (newNode){
		cout << newNode->val << " ";
		newNode = newNode->next;
	}
	system("pause");
	return 0;
}

链表翻转:

#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <map>

using namespace std;
struct ListNode
{
	ListNode* next;
	int val;
	ListNode(int _val = 0){ 
		val = _val;
		next = NULL;
	}
};

void print(ListNode* l){
	ListNode* cur = l;
	while (cur){
		cout << cur->val << " ";
		cur = cur->next;
	}
	return;
}

ListNode *reverse(ListNode *l){
	
	if (!l || !(l->next)) return l;
	ListNode *dummy = new ListNode(0);
	dummy->next = l;
	ListNode *cur = dummy->next;
	while (cur->next)
	{
		ListNode *pre = cur->next;
		cur->next = pre->next;
		pre->next = dummy->next;
		dummy->next = pre;
	}
	return dummy->next;
}
int main() {

	Solution sol = Solution();
	ListNode *l1 = new ListNode(7);
	ListNode *l2 = new ListNode(5);
	ListNode *l3 = new ListNode(6);
	ListNode *l4 = new ListNode(2);
	l1->next = l2;
	l2->next = l3;
	l3->next = l4;

	ListNode* newNode = reverse(l1);

	while (newNode){
		cout << newNode->val << " ";
		newNode = newNode->next;
	}
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值