本人转码小白,正在学习算法,欢迎各位大佬批评指正。算法思想来自博客代码随想录。
链接:代码随想录
1.题目描述:
给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表
2.题解:
92题是206题的一个升级版,但是核心算法不变。
- 把子链表截取出来,原链表变成了三部分:1->null; 2->3->4->null; 5;
- 子链表翻转:2->3->4->null 变成 4->3->2->null。(就是92题)
- 首尾连接,恢复完整的链表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;
}