92. Reverse Linked List II
2018-01-26 16:04
232 查看
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(head==NULL) return head;
ListNode* tail = head;
ListNode* temp=new ListNode(0);
ListNode* pre;
ListNode* temp1;
temp->next = head;
pre = temp;
int pos = 1;
ListNode* cursor;
while(m>pos){
tail = tail->next;
temp = temp->next;
pos++;
}
cursor = tail->next;
while(pos<n&&cursor!=NULL){
temp1 = temp->next;
//printList(pre);
temp->next = cursor;
cursor=cursor->next;
tail->next = cursor;
temp->next->next = temp1;
pos++;
}
return pre->next;
}
void printList(ListNode* L1){
while(L1!=NULL){
cout<<L1->val;
L1 = L1->next;
}
cout<<endl;
}
};
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(head==NULL) return head;
ListNode* tail = head;
ListNode* temp=new ListNode(0);
ListNode* pre;
ListNode* temp1;
temp->next = head;
pre = temp;
int pos = 1;
ListNode* cursor;
while(m>pos){
tail = tail->next;
temp = temp->next;
pos++;
}
cursor = tail->next;
while(pos<n&&cursor!=NULL){
temp1 = temp->next;
//printList(pre);
temp->next = cursor;
cursor=cursor->next;
tail->next = cursor;
temp->next->next = temp1;
pos++;
}
return pre->next;
}
void printList(ListNode* L1){
while(L1!=NULL){
cout<<L1->val;
L1 = L1->next;
}
cout<<endl;
}
};
相关文章推荐
- leecode 解题总结:92. Reverse Linked List II
- 【leetcode】92. Reverse Linked List II
- 92. Reverse Linked List II(链表局部逆序**)
- LeetCode OJ 92. Reverse Linked List II
- 92. Reverse Linked List II
- leetcode 92. Reverse Linked List II
- Leetcode 92. Reverse Linked List II (Medium) (cpp)
- 92. Reverse Linked List II
- LeetCode 92. Reverse Linked List II
- leetcode题解-92. Reverse Linked List II
- [leetcode] 【链表】92. Reverse Linked List II
- 92. Reverse Linked List II
- 92. Reverse Linked List II
- 92. Reverse Linked List II
- 92. Reverse Linked List II
- Leetcode 92. Reverse Linked List II
- Leetcode 92. Reverse Linked List II 翻转链表2 解题报告
- 【LeetCode】92. Reverse Linked List II
- Leetcode 92. Reverse Linked List II
- [LeetCode] 92. Reverse Linked List II