题目:

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.

反转m到n处的单链表

The basic idea is as follows:

(1) Create a new_head that points to head and use it to locate the immediate node before them-th (notice that it is 1-indexed) node pre;

(2) Set cur to be the immediate node after pre and at each time move the immediate node after cur (named move) to be the immediate node after pre. Repeat it for n - m times.

分类:Linked List

代码:

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

[LeetCode92]Reverse Linked List II的更多相关文章

  1. Leetcode92: Reverse Linked List II 翻转链表问题

    问题描述 给定一个链表,要求翻转其中从m到n位上的节点,返回新的头结点. Example Input: 1->2->3->4->5->NULL, m = 2, n = 4 ...

  2. Leetcode92. Reverse Linked List II反转链表

    反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m = 2 ...

  3. LeetCode 92. 反转链表 II(Reverse Linked List II)

    92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...

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

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

  6. 【原创】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-p ...

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

  8. lc面试准备:Reverse Linked List II

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

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

随机推荐

  1. C++ 多态性分析

    编译 - 时间多态性--函数重载 编译后的中间代码(例如GCC产生.o文件.此时还不是汇编语言)函数名字有变化,看以下两个样例. void cc_show(const char*str)     -& ...

  2. ad nbetmk57

    http://www.zhihu.com/collection/24337307 http://www.zhihu.com/collection/24337259 http://www.zhihu.c ...

  3. git不同linux版本号说明

    在确保你安装好git后,我们就能够通过git来下载linux kernel了,这时要先说一下linux的版本号分类. 在 Linux 内核官网上(https://www.kernel.org/),我们 ...

  4. Android中怎样在应用A中启动或安装应用B

    看到别人做的游戏攻略,想着自己的游戏攻略也加入新的功能,即Android中怎样在应用A中启动或安装应用B.就查了一些资料整理下来. 启动或安装对应的应用的方法: Step1:推断是否安装目标应用.仅仅 ...

  5. SE 2014年4月18日

    实验需求:   R1 R2 R3用环回口建立IBGP对等体(使用对等体组),AS号为100                     R4 R5 R6用环回口建立IBGP对等体(使用对等体组),AS号为 ...

  6. AJAX POST请求中參数以form data和request payload形式在servlet中的获取方式

    HTTP请求中,假设是get请求,那么表单參数以name=value&name1=value1的形式附到url的后面,假设是post请求,那么表单參数是在请求体中,也是以name=value& ...

  7. 为了找到自己的路——leo锦书62

    <Leo锦书(文章1编辑)>百度已经降落阅读,今后将继续更新.免费下载:http://t.cn/RvawZEx 柯克•卡梅隆是谁呢?在中国听过这名字的人预计不多.但看下封面我们马下就会说: ...

  8. Jndi使用好处,与简单实例【JBOSS】

    JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一,不少专家认为,没有透彻理解JNDI的意义和作用,就没有 ...

  9. iOS开发人员必看的精品资料(100个)

    目录下载地址:http://down.51cto.com/data/416672 附件预览如下: iOS游戏开发之Unity3D引擎系列 http://down.51cto.com/zt/399 iP ...

  10. 使用OGG&quot;Loading data from file to Replicat&quot;的方法应该注意的问题:replicat进程是前台进程

    使用OGG的 "Loading data from file to Replicat"的方法应该注意的问题:replicat进程是前台进程 因此.最好是在vncserver中调用该 ...