Reverse Linked List II

原创 2015年11月19日 23:56:36

题目: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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

思路:

这一题比较巧妙,实际上是这样的一个循环:





如图所示,通过循环找到第m个,标记为f,f的下一个标记为t,f指向f 的next的next,s指向t,t 指向f.这是第一次,

然后呢,f 还是指向f 的next的next,slow 指向刚刚f 的next,也就是被标记的t ,如此一来,便完成循环。

代码:

/**
 * 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) {
        /*ListNode *dummy=new ListNode(0);
        dummy->next = head;
        ListNode *slow = dummy;
        n -= m;
        while (--m)
            slow = slow->next;
        ListNode *fast = slow->next, *tmp;
        while (n--) {
            tmp = fast->next;
            fast->next = fast->next->next;
            tmp->next = slow->next;
            slow->next = tmp;
        }
        return dummy->next;*/
        
        ListNode *dummy=new ListNode(0);
        dummy->next=head;
        
        ListNode *slow=dummy;
        n=n-m;//这一句话不能够放在下面循环的后面,因为m变了。
        while(--m){
            slow=slow->next;//在m个之前的一个
        }
        
        ListNode *fast=slow->next,*tmp;
        while(n--){
            tmp=fast->next;
            fast->next=fast->next->next;
            
            tmp->next= slow->next;
            slow->next=tmp;
            //fast=slow->next;
        }
        return dummy->next;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

( Leetcode 92 ) Reverse Linked List II

题目:Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one...

LeetCode Reverse Linked List II

题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Giv...

LeetCode 92. Reverse Linked List II

1. 题目描述 Reverse a linked list from position m to n. Do it in-place and in one-pass. For examp...

[LeetCode] Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1...
  • game115
  • game115
  • 2014年04月17日 23:29
  • 712

leetcode -day30 Reverse Linked List II

1、  Reverse Linked List II  Reverse a linked list from position m to n. Do it in-place and in...

Reverse Linked List II leetcode

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1...

Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1...

leetcode No92. Reverse Linked List II

Question: Reverse a linked list from position m to n. Do it in-place and in one-pass. For exam...

【Leetcode】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2...

[LeetCode] Reverse Linked List II Solution

Reverse a linked list from position m to n, Do it in-place and in one-pass. For example:  Given 1-...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Reverse Linked List II
举报原因:
原因补充:

(最多只允许输入30个字)