reverse-linked-list-ii
描述
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
要点
逆转其中固定数目的链表
解法
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode *dummy =new ListNode(0),pre =dummy;
dummy->next=head;
for(int i =1 ;i<m;i++){
pre=pre->next;
}
ListNode * cur = pre ->next;
for (int i=0;i<n-m;i++)
{
ListNode move =cur->next;
cur->next=move->next;
move->next=pre->next;
pre->next=move;
}
return dummy->next;
}
};