首页 > 代码库 > LeetCode "Reverse Linked List II"
LeetCode "Reverse Linked List II"
Just corner case..
class Solution {public: ListNode *reverseBetween(ListNode *head, int m, int n) { if(m == n) return head; ListNode *pPre = NULL; ListNode *p1 = head; ListNode *p2 = head->next; ListNode *pPost = NULL; if(p2) pPost = p2->next; // Find m first int om = m; while(--om) { if(!pPre) pPre = head; else pPre = pPre->next; p1 = p2; if(p2) p2 = p2->next; if(pPost) pPost = pPost -> next; } ListNode *pPreM = pPre; ListNode *pM = p1; // Reverse it int cnt = n - m; while(cnt --) { p1->next = pPre; p2->next = p1; pPre = p1; p1 = p2; p2 = pPost; if(pPost) pPost = pPost->next; } if(pPreM) pPreM->next = p1; else head = p1; if(pM) pM->next = p2; return head; }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。