LeetCode 92_翻转链表II c++本地实现(超详细)

本人转码小白,正在学习算法,欢迎各位大佬批评指正。算法思想来自博客代码随想录。

链接:代码随想录

1.题目描述:

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表

2.题解:

92题是206题的一个升级版,但是核心算法不变。

  1. 把子链表截取出来,原链表变成了三部分:1->null; 2->3->4->null; 5;
  2. 子链表翻转:2->3->4->null 变成 4->3->2->null。(就是92题)
  3. 首尾连接,恢复完整的链表1->4->3->2->5;

3.本地实现

手动定义链表

struct ListNode
{
	int val;
	ListNode* next;
	ListNode(int x) : val(x), next(NULL) {};
};

创建一个链表1->2->3->4->5

ListNode* creatListNode()
{
	ListNode* head = NULL;
	ListNode* cur = NULL;
	vector<int> nums = { 1,2,3,4,5 };
	for (int i = 0; i < nums.size(); i++)
	{
		if (head == NULL)
		{
		    head = new ListNode(nums[i]);
			cur = head;
		}
		else
		{
			cur->next = new ListNode(nums[i]);
			cur = cur -> next;
		}
	}
	return head;
}

打印链表 看看结果

void printListNode(ListNode* head)
{
	ListNode* cur = head;
	while (cur != nullptr)
	{
		cout << cur->val << " ";
		cur = cur->next;
	}
}

翻转链表

class Solution
{
public:
	ListNode* reverseList(ListNode* head, int m, int n)
	{
		//先找一下区间边界
		ListNode* dummyhead = new ListNode(0); //为了方便,设置了一个虚拟的头结点。
		dummyhead->next = head;
		ListNode* right = dummyhead;
		ListNode* left = dummyhead;
		ListNode* pre = NULL;
		for (int i = 0; i < m; i++)
		{
			pre = left; //保存左边界的前一个,即1
			left = left->next;//找到左边界,即2
		}
		for (int i = 0; i < n; i++)
		{
			right = right->next;//右到左边界,即4
		}
		ListNode* final = right->next;//保存右边界的后一个,即5

		//切断链表,得到子链表 2->3->4->null
		pre->next = NULL; //1->null
		right->next = NULL;

		//翻转子链表,得到链表 4->3->2->null
		ListNode* pree = NULL;
		ListNode* temp = NULL;
		ListNode* cur = left;
		while (cur)
		{
			temp = cur->next;
			cur->next = pree;
			pree = cur;
			cur = temp;
		}

		//链表复原 
		pre->next = pree; //把1指向4, 1->4->3->2
		left->next = final;//把2指向5, 1->4->3->2->5 
		return dummyhead->next;
	}
};

主函数

int main()
{
	Solution solution;
	ListNode* test = creatListNode();
	ListNode* res = solution.reverseList(test,2,4);
	printListNode(res);
	system("pause");
	return 0;
}

因为本人比较蠢,还经常想找一些一复制就能跑的代码。所以放一个完整版。

#include<iostream>
#include<vector>
using namespace std;

struct ListNode{
	int val;
	ListNode* next;
	ListNode(int x) : val(x), next(NULL) {};
};

ListNode* creatListNode(){
	ListNode* head = NULL;
	ListNode* cur = NULL;
	vector<int> nums = { 1,2,3,4,5 };
	for (int i = 0; i < nums.size(); i++){
		if (head == NULL){
			head = new ListNode(nums[i]);
			cur = head;
		}
		else{
			cur->next = new ListNode(nums[i]);
			cur = cur->next;
		}
	}
	return head;
}

void printListNode(ListNode* head){
	ListNode* cur = head;
	while (cur != nullptr){
		cout << cur->val << " ";
		cur = cur->next;
	}
}

class Solution
{
public:
	ListNode* reverseList(ListNode* head, int m, int n){
		ListNode* dummyhead = new ListNode(0); 
		dummyhead->next = head;
		ListNode* right = dummyhead;
		ListNode* left = dummyhead;
		ListNode* pre = NULL;
		for (int i = 0; i < m; i++){
			pre = left; 
			left = left->next;
		}
		for (int i = 0; i < n; i++){
			right = right->next;
		}
		ListNode* final = right->next;
		pre->next = NULL;
		right->next = NULL;
		ListNode* pree = NULL;
		ListNode* temp = NULL;
		ListNode* cur = left;
		while (cur){
			temp = cur->next;
			cur->next = pree;
			pree = cur;
			cur = temp;
		}
		pre->next = pree; 
		left->next = final;
		return dummyhead->next;
	}
};

int main(){
	Solution solution;
	ListNode* test = creatListNode();
	ListNode* res = solution.reverseList(test,2,4);
	printListNode(res);
	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值