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。

1<=m<=n<=length。

解题思路:

就我目前学习到的,有用指向指针的指针的,有插入,有逆转的。但是所有方法的核心,都其实是一个算法,就是利用3个指针修改区间内的节点的next值,且要保证已修改的链表是可以被找到的,即可以连入原链表中。

假设有一个链表有1,2,3,4,5,6,6个节点。m=2,n=5。

我们添加一个dummy节点,方便操作第一个节点。

首先让pre指向第m个节点前面那个,cur指向第m个节点,p1指向m的下一个节点,因为p1有可能是NULL,所以当p1不是NULL的时候,p2指向p1的下一个节点。

上图画出了这几个指针指向情况的开始状态和我们希望的终止状态。

在最终状态下,再通过其中方框中的两步就可以我们需要的链表了。

而cur,p1,p2三个指针在区间内向前移动并且将p1的next指向cur的过程则包含在一个for循环内部。如下:

代码如下:

 class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode *dummy = new ListNode();
dummy->next = head; ListNode *pre = dummy, *cur = head;
for(int i = ; i < m; i++){
pre = cur;
cur = cur->next;
} ListNode *p1,*p2;
if(cur)
p1 = cur->next;
if(p1)
p2 = p1->next;
for(int j = m; j < n; j++){
p1->next = cur;
cur = p1;
p1 = p2;
if(p2) p2 = p2->next;
}
pre->next->next = p1;
pre->next = cur; return dummy->next;
}
};

【LeetCode练习题】Reverse Linked List II的更多相关文章

  1. 【leetcode】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. F ...

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

  3. [LeetCode] 92. Reverse Linked List II 反向链表II

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

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

  5. [LeetCode] 92. Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

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

  7. leetcode 92 Reverse Linked List II ----- java

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

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

  9. LeetCode 92. Reverse Linked List II倒置链表2 C++

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  10. [leetcode]92. Reverse Linked List II反转链表2

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

随机推荐

  1. LeeCode-Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  2. 【错误】:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'

    错误:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决 ...

  3. 熬之滴水穿石:Spring--精简的J2EE(5)

                                   47--Spring的MVC 在Spring的框架中也存在MVC这样的模式,在Spring下有2个这样的控制器一个叫Controller, ...

  4. 谱聚类--SpectralClustering

    谱聚类通常会先对两两样本间求相似度. 然后依据相似度矩阵求出拉普拉斯矩阵,然后将每一个样本映射到拉普拉斯矩阵特诊向量中,最后使用k-means聚类. scikit-learn开源包中已经有现成的接口能 ...

  5. react redux 相关技术

    React全都是围绕着组件的, 所以React基础也就是:写组件的jsx.组件的生命周期以及组件的属性和状态.jsx,只要是用过html模板的分分钟就能写了: 所谓生命周期就是组件在创建.销毁.更新阶 ...

  6. Java之可变参数

    Java中支持可变参数 意思就是:参数的个数可以根据需要写,你可以写1个.2个.3个....他们都被保存到一个参数的数组中. 但是这些参有一些约束:他们必须是同类型的,比如都是String字符串类型. ...

  7. php基础之 ->, =>,@,&,::,%符号

    => 是数组成员访问符号 -> 是对象成员访问符号 比如: $array = array("site map"=>"map.php"); // ...

  8. 理解Java的GC日志

    分析如下GC日志:[GC [PSYoungGen: 9216K->1024K(9216K)] 1246196K->1246220K(1287040K), 0.2398360 secs] [ ...

  9. mysql单表大小的限制

    mysql单表大小的限制一.MySQL数据库的MyISAM存储 引擎单表大小限制已经不是有MySQL数据库本身来决定(限制扩大到64pb),而是由所在主机的OS上面的文件系统来决定了.在mysql5. ...

  10. MySQL查询优化之explain

    在分析查询性能时,考虑EXPLAIN关键字同样很管用.EXPLAIN关键字一般放在SELECT查询语句的前面,用于描述MySQL如何执行查询操作.以及MySQL成功返回结果集需要执行的行数.expla ...