leetcode Reverse Linked List II
来源:互联网 发布:压缩文件解压软件 编辑:程序博客网 时间:2024/06/07 06:26
这道题是给定了位置的逆序。
一.这道题主要是对于少于等于2个节点 和 多于2个节点的情况进行区别。少于2个节点的时候presubhead 是NULL。
当presubhead是null的时候,tail才是头。
presubhead不是null,head才是头。
二.代码
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode *subhead,*presubhead,*tail;
ListNode *cur,*pre,*temp;
subhead = head;
presubhead = NULL;
int i=1;
if(!head)
return NULL;
if(m==n)
return head;
while(i!=m)
{
presubhead = subhead;
subhead = subhead->next;
i++;
} //结束后subhead presubhead 即指向第m 和m-1
cur = subhead->next;
pre = subhead;
while(i!=n)
{
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
i++;
}
tail = pre; //cur存着tail的next
if(presubhead!=NULL)
{
presubhead->next = tail;
subhead->next = cur;
return head;
}
else
{
subhead->next =cur;
return tail;
}
}
};
- LeetCode[Linked List]: Reverse Linked List II
- LeetCode: Reverse Linked List II
- LeetCode: Reverse Linked List II
- [LeetCode] Reverse Linked List II
- [Leetcode] Reverse Linked List II
- [LeetCode]Reverse Linked List II
- Leetcode: Reverse Linked List II
- [Leetcode]Reverse Linked List II
- [leetcode]Reverse Linked List II
- LeetCode-Reverse Linked List II
- LeetCode Reverse Linked List II
- [leetcode] Reverse Linked List II
- LeetCode - Reverse Linked List II
- [LeetCode] Reverse Linked List II
- LeetCode | Reverse Linked List II
- 【leetcode】Reverse Linked List II
- Leetcode: Reverse Linked List II
- leetcode Reverse Linked List II
- 初步认识React
- android 前后台流量监测
- Poj 2151 Check the difficulty of problems【概率dp+活用补集】
- session和cookie的区别
- 【敏捷开发每日一贴】代码走查
- leetcode Reverse Linked List II
- vmware虚拟机三种网络模式详解
- webstorm mac 安装
- HDU 1711 Number Sequence(KMP)
- Java数据类型面试题目
- 完美!下拉刷新(上拉加载)框架--弹性效果实现
- PL/SQL程序的变量类型
- CUDA编程系列--GPU架构,由sp,sm,thread,block,grid,warp说起
- mongo(一)