标签:描述 inpu ret lis head tco amp i++ eve
算法描述:
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
解题思路:链表题,首先要画图。四个指针,头指针,前指针,临时指针。
ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode* dummy = new ListNode(-1); dummy->next = head; ListNode* prev = dummy; ListNode* cur = dummy; for(int i=1; i < m; i++) prev = prev->next; cur = prev->next; for(int i =m; i<n; i++){ ListNode* temp = cur->next; cur->next = temp->next; temp->next =prev->next; prev->next = temp; } return dummy->next; }
LeetCode-92-Reverse Linked List II
标签:描述 inpu ret lis head tco amp i++ eve
原文地址:https://www.cnblogs.com/nobodywang/p/10348235.html