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->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个元素

然后计算需要交换的元素个数,n-m

每次交换时,把下一个元素交换到需要交换的初始位置

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

找到交换位置

 1->->3->4->5->NULL

把下一个元素交换到需要交换的初始位置

1->->2->4->5->NULL

继续交换

1->->3->2->5->NULL

注意初始位置在开头时,每次交换都要更新head

 /**
* 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 *p1=head;
ListNode *p1Pre=new ListNode(); int k=n-m; p1Pre->next=p1; ListNode *needDelete=p1Pre; while(m->)
{
p1Pre=p1;
p1=p1->next;
m--;
} while(k>)
{
ListNode* cur=p1->next;
p1->next=cur->next; if(p1Pre->next==head)
{
head=cur;
} cur->next=p1Pre->next;
p1Pre->next=cur; k--;
} delete needDelete;
return head;
}
};

【leetcode】Reverse Linked List II的更多相关文章

  1. 【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. 【leetcode】Reverse Linked List(easy)

    Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...

  3. 【链表】 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- ...

  4. 【LeetCode92】Reverse Linked List II★★

    题目描述: 解题思路: 题目大意:给定一个链表,反转第m到第n个结点部分,m.n满足1 ≤ m ≤ n ≤ length of list. 解题思路参照LeetCode206题,用迭代法,不过要注意以 ...

  5. 【LeetCode】链表 linked list(共34题)

    [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...

  6. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  7. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  8. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  9. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

随机推荐

  1. MVC缓存OutPutCache学习笔记 (二) 缓存及时化VaryByCustom

    <MVC缓存OutPutCache学习笔记 (一) 参数配置> 本篇来介绍如何使用 VaryByCustom参数来实现缓存的及时化.. 根据数据改变来及时使客户端缓存过期并更新.. 首先更 ...

  2. idea的修改文件变颜色

    直接用IntelliJ IDEA 拉SVN 工程 时,如下图所示都不勾选可以成功拉取.

  3. 关于http请求

    response的Head值:  200 : 请求已成功,请求所希望的响应头或数据体将随此响应返回  304 : Not Modified 客户端有缓冲的文档并发出了一个条件性的请求,原来缓冲的文档还 ...

  4. 【转】Flume日志收集

    from:http://www.cnblogs.com/oubo/archive/2012/05/25/2517751.html Flume日志收集   一.Flume介绍 Flume是一个分布式.可 ...

  5. python基础教程1

    python作为一种编程语言,诞生于1990年左右,算是一门比较年轻的语言(也算是90后吧),它是面向对象的,但不同于JAVA\C#那么严格要求一切皆对象,更接近于C++,是面向过程和面向对象的结合: ...

  6. (1)apply族函数总论

                  来自为知笔记(Wiz) 附件列表

  7. PHP安装laravel(win+linux)

    作为一名不优秀的程序猿,忙碌的四月终于结束了,五一大假的最后一天,终于有时间来整理整理这段时间的收获了. 一.laravel介绍 首先看看http://www.sitepoint.com/网站做的一个 ...

  8. Back to Edit Distance(LCS + LIS)

    Given 2 permutations of integers from 1 to N, you need to find the minimum number of operations nece ...

  9. jquery选择器(二)-层次选择器

    1. 后代选择器 2. 子元素选择器 3. 相邻兄弟元素选择器 4. 一般兄弟元素选择器(同辈元素选择器) 5. 补充(除自身以外的前后同辈选择器)

  10. 【C语言入门教程】1.2 函数库 和 链接

    程序员可以不需要从头开始设计每一个函数,完成用C语言命令所实现的函数非常罕见.因为所有的C语言编辑器都提供能完成各种常见任务函数,如printf()函数等.C语言编译器的实现者已经编写了大部分常见的通 ...