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 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.

Algorithm:

见程序,也是参照了大神的解法

Accepted Code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
/*
Ex:
	    1->2->3->4->5->NULL
return: 1->4->3->2->5->NULL
*/
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(head==NULL)return head;
        ListNode* mpre=NULL;      //Node before No.m
        ListNode* p=head;

        for(int i=1;i<m;i++)
        {
            mpre=p;
            p=p->next;
        }
/*循环结束后,mpre指向需要reverse的前一个结点,p指向需要reverse的第一个结点,即第m-1个结点
mpre   data为1的结点
p      data为2的结点
*/
        ListNode* nend=p;          //记录下此时的p,reverse后,这个结点循环后是第n个结点
        ListNode* ppre=p;          //记录下此时的p,用来交换,这个结点循环后是第m个结点
        p=p->next;
/*
nend   data为2的结点
ppre   data为2的结点
p      data为3的结点
*/
        for(int i=m;i<n;i++)         //swap ppre and p
        {
            ListNode* tmp=p->next;
            p->next=ppre;
            ppre=p;
            p=tmp;
        }
/*
p      data为5的结点
ppre   data为4的结点
nend   data为2的结点
*/
        nend->next=p;       //nend接上
        if(mpre)            //mpre接上
            mpre->next=ppre;
        else
            head=ppre;
        return head;
    }
};
时间: 2024-08-27 07:23:16

leetcode No92. Reverse Linked List II的相关文章

LeetCode OJ - 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->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 ≤ l

【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->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 ≤ lengt

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 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 follow

[LeetCode][JavaScript]Reverse Linked List II

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->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following cond

LeetCode Solutions : Reverse Linked List II

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->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following co

Java for LeetCode 092 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->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 ≤ le

leetcode 【 Reverse Linked List II 】 python 实现

题目: 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 ≤ l

【leetcode】Reverse Linked List II (middle)

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. 思路: 好困啊,脑子晕晕的. 转了半天AC了.但写的很罗嗦,要学习大神的写法. 注意翻转的写法. 用伪头部 大神14行简洁代码 L

leetcode 91. 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->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 ≤ lengt