题目
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL
, m = 2 and n = 4,
return 1->4->3->2->5->NULL
.
Note:
Given m, n satisfy the following condition:
1 ? m ? n ? length of list.
思路
这里需要操作连续的三个节点,分别是 pre cur 和 nxt ,->口->口->口
这里 用到了4个指针,revtail 和 revhead 表示翻转后的子链表的头和尾(即m节点和n节点),newhead 和 newtail 表示翻转的起始节点和结束节点(即m-1节点和n+1节点);
代码一:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!head || m>=n)
return head;
ListNode *pre = head;
ListNode *cur = head;
ListNode *nxt = cur->next;
ListNode *revtail = NULL;
ListNode *revhead = NULL;
ListNode *newtail = NULL;
ListNode *newhead = NULL;
int i = 1;
while(cur)
{
nxt = cur->next;
if(i==m)
{
revtail = cur;
newhead = pre;
pre->next = NULL;
}
if(i>m && i<n)
{
cur->next = pre;
}
if(i==n)
{
cur->next = pre;
revhead = cur;
newtail = nxt;
}
i++;
pre = cur;
cur = nxt;
}
if(newhead!=revtail)
{
newhead->next = revhead;
revtail->next = newtail;
return head;
}
else
{
revtail->next = newtail;
return revhead;
}
}
};
代码二:这里我申请了一个Dump节点作为附加的头结点。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!head || m>=n)
return head;
ListNode dump(0);
dump.next = head;
ListNode *pre = &dump;
ListNode *cur = head;
ListNode *nxt = cur->next;
ListNode *revtail = NULL;
ListNode *revhead = NULL;
ListNode *newtail = NULL;
ListNode *newhead = NULL;
int i = 1;
while(cur && i<=n)
{
nxt = cur->next;
if(i==m)
{
revtail = cur;
newhead = pre;
}
if(i>m && i<n)
{
cur->next = pre;
}
if(i==n)
{
cur->next = pre;
revhead = cur;
newtail = nxt;
}
i++;
pre = cur;
cur = nxt;
}
newhead->next = revhead;
revtail->next = newtail;
return dump.next;
}
};
代码三:这里减少重复的指针,尽量少的利用指针变量
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!head || m>=n)
return head;
ListNode dump(0);
dump.next = head;
ListNode *pre = &dump;
ListNode *cur = head;
ListNode *nxt = NULL;
for(int i=1;i<m;i++)
{
pre = cur;
cur = cur->next;
}
ListNode *newhead = pre;
ListNode *newtail = cur;
pre = cur;
cur = cur->next;
int j = m+1;
while(cur && j++<=n)
{
nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
newtail->next = cur;
newhead->next = pre;
return dump.next;
}
};