Reverse Linked List II
生活随笔
收集整理的這篇文章主要介紹了
Reverse Linked List II
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Reverse a linked list from position?m?to?n. Do it in-place and in one-pass.
For example:
Given?1->2->3->4->5->NULL,?m?= 2 and?n?= 4,
return?1->4->3->2->5->NULL.
Note:
Given?m,?n?satisfy the following condition:
1 ≤?m?≤?n?≤ length of list.
相當(dāng)于部分區(qū)域旋轉(zhuǎn)
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode* reverseBetween(ListNode* head, int m, int n) {if (head == NULL)return NULL;ListNode* Head=new ListNode(NULL);Head->next=head;ListNode* pre=Head;for(int i=0;i<m-1;i++)pre=pre->next;//找到第一個平移的節(jié)點,移動ListNode* head2=pre;pre=pre->next;ListNode* cur=pre->next;//平移一個一個節(jié)點for(int i=m;i<n;i++){pre->next=cur->next;cur->next=head2->next;head2->next=cur;cur=pre->next;}return Head->next;} };
總結(jié)
以上是生活随笔為你收集整理的Reverse Linked List II的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Reorder List
- 下一篇: 矩形覆盖