BM2. 链表内指定区间反转
描述
示例1
输入:
{1,2,3,4,5},2,4
输出:
{1,4,3,2,5}
示例2
输入:
{5},1,1
输出:
{5}
C++ 解法, 执行用时: 2ms, 内存消耗: 336KB, 提交时间: 2021-05-30
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ ListNode* reverseBetween(ListNode* head, int m, int n) { // write code here if(m == n) return head; ListNode *dum = new ListNode(-1); dum->next = head; ListNode *cur = dum; for(int i = 0; i < m - 1; ++i) cur = cur->next; ListNode *tmp = cur->next; ListNode *ppre = tmp; ListNode *pnode = tmp->next; ListNode *pnext = NULL; for(int i = m + 1; i <= n; ++i) { pnext = pnode->next; pnode->next = ppre; if(i == n) { tmp->next = pnext; if(m == 1) head = pnode; else cur->next = pnode; } ppre = pnode; pnode = pnext; } return head; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 340KB, 提交时间: 2021-04-04
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ ListNode* reverseBetween(ListNode* head, int m, int n) { // write code here int len=n-m+1; int m0=m; ListNode *p=head; ListNode *pre; while(--m) { pre=p; p=p->next; } ListNode *pn=head; while(n--) { pn=pn->next; } ListNode* p1=pn; while(p&&len--) { ListNode *tem=p->next; p->next=p1; p1=p; p=tem; } if(m0>1) { pre->next=p1; return head; } else { return p1; } } };
C++ 解法, 执行用时: 2ms, 内存消耗: 340KB, 提交时间: 2020-12-27
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode dummy(-1); dummy.next = head; ListNode* p = &dummy; ListNode* pre = nullptr; for(int i=0;i<m;++i) { pre = p; p = p->next; } ListNode* newTail = p; ListNode* q = p; p = p->next; for(int j=m;j<n;++j) { auto next = p->next; p->next = q; q = p; p = next; } newTail->next = p; if(pre) { pre->next = q; }else{ head = q; } return dummy.next; } ListNode* reverseList(ListNode* head) { if(head==nullptr || head->next == nullptr) return head; ListNode* cur=head; ListNode* pre=nullptr; while(cur) { auto next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 344KB, 提交时间: 2021-03-19
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { // write code here if(m==n){return head;} ListNode Front(-1); Front.next = head; ListNode* MPre = &Front; for(int i = 0 ; i < m-1 ; i++) { MPre = MPre->next; } ListNode* M = MPre->next; ListNode* Pre = M; ListNode* Now = M->next; ListNode* Next = NULL; for(int i = m+1 ; i <= n ; i++){ Next = Now->next; Now->next = Pre; if(i == n) { M->next = Next; if(m == 1){ head = Now; } else{ MPre->next = Now; } } Pre = Now; Now = Next; } return head; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 344KB, 提交时间: 2020-12-02
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ ListNode* reverseBetween(ListNode* head, int m, int n) { if(!head || head->next == nullptr) { return head; } ListNode *p = head, *q = nullptr; for(int i = 0; i < m-1; i++) { q = p; p = p->next; } ListNode *rend = p, *next = nullptr; ListNode *prev = p; p =p->next; for(int i = m; i < n; i++) { next = p->next; p->next = prev; prev = p; p = next; } rend->next = p; if(q) { q->next = prev; } else { head = prev; } return head; } };