92. Reverse Linked List II

2人阅读 评论(0) 收藏 举报

1.问题描述

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.

来自 https://leetcode.com/problems/reverse-linked-list-ii/description/

2.题目分析

题目分析:链表中区间为[m,n]的元素进行反转,因此我们先定位到这个区间(leftpre,right],使用206题的方法进行反转。最后进行两个链表的拼接。

3.c++代码

//我的代码:(beats 100%)
ListNode* reverseBetween(ListNode* head, int m, int n)
{
    if (head == NULL)return head;
    ListNode*dummy = new ListNode(0);
    dummy->next = head;
    ListNode*leftpre = dummy;
    ListNode*right = dummy;
    for (int i = m; i <= n; i++)
        right = right->next;
    for (int j = 1; j < m; j++)
    {
        leftpre = leftpre->next;
        right = right->next;
    }
    //leftpre->next = right->next;
    ListNode*newhead = new ListNode(leftpre->next->val);
    newhead->next = right->next;
    right->next = NULL;
    ListNode*p = leftpre->next->next;
    ListNode*tmp;
    while (p)
    {
        tmp = p->next;
        p->next = newhead;//头插法
        newhead = p;
        p = tmp;
    }
    leftpre->next = newhead;
    return dummy->next;
}
查看评论

leetcode:92. Reverse Linked List II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705 题目地址:https://leetcode.com/problems/rever...
  • u012975705
  • u012975705
  • 2015-12-26 16:14:34
  • 1844

【LeetCode】92. Reverse Linked List II 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2017-03-28 09:42:23
  • 1117

Reverse Linked List II -- LeetCode

原题链接: http://oj.leetcode.com/problems/reverse-linked-list-ii/  这道题是比较常见的链表反转操作,不过不是反转整个链表,而是从m到n的一部...
  • linhuanmars
  • linhuanmars
  • 2014-04-28 04:58:33
  • 13507

LeetCode(92)ReverseLinkedList2

题目如下: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given...
  • feliciafay
  • feliciafay
  • 2014-01-28 01:06:19
  • 2587

[LeetCode]92.Reverse Linked List II

【题目】 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Giv...
  • SunnyYoona
  • SunnyYoona
  • 2014-01-28 11:06:09
  • 2705

92. 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->...
  • qq_28119401
  • qq_28119401
  • 2016-09-18 21:54:15
  • 138

leetcode 206: Reverse Linked List

Reverse Linked List leetcode
  • xudli
  • xudli
  • 2015-05-14 06:47:30
  • 13881

【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...
  • ljiabin
  • ljiabin
  • 2014-12-05 15:59:28
  • 4411

leetcode笔记--Reverse Linked List

前话:今天开始刷leetcode的第一道题,我所选用的语言是较为熟悉的python,其实自己似乎并没有一门十分精通的语言,在大学时候有系统学过c语言,但由于后面实践中实际上用的很少,现在很多特性都已经...
  • u010366748
  • u010366748
  • 2016-02-19 13:39:20
  • 608

Leetcode解题-链表(2.2.2)ReverseLinkedList

题目:2.2.2 Reverse Linked List IIReverse a linked list from position m to n. Do it in-place and in one...
  • dc_726
  • dc_726
  • 2015-03-28 11:17:21
  • 1921
    个人资料
    等级:
    访问量: 25
    积分: 50
    排名: 179万+
    文章分类
    文章存档