题目:

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

代码:

  1. class Solution {
  2. public:
  3. ListNode* reverseBetween(ListNode* head, int m, int n) {
  4. ListNode* new_head = new ListNode();
  5. new_head -> next = head;
  6. ListNode* pre = new_head;
  7. for (int i = ; i < m - ; i++)
  8. pre = pre -> next;
  9. ListNode* cur = pre -> next;
  10. for (int i = ; i < n - m; i++) {
  11. ListNode* move = cur -> next;
  12. cur -> next = move -> next;
  13. move -> next = pre -> next;
  14. pre -> next = move;
  15. }
  16. return new_head -> next;
  17. }
  18. };

[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. 百度词典搜索_dress code

    百度词典搜索_dress code dress code n.着装标准

  2. ADN中国团队參加微软的Kinect全国大赛获得三等奖

    上周末我们团队參加了微软的Kinect全国大赛,我们的Kinect + Navisworks漫游荣膺三等奖   团队经理Joe写了篇详实的总结,我就直接转载了. http://blog.csdn.ne ...

  3. hdu 1251 统计难题 (map水过)

    # include <stdio.h> # include <algorithm> # include <string> # include <map> ...

  4. UVA 5875 DP

    题意:给你一堆二维点,每个点有一些分数. 现在要从点(0 , 0 )出发,只能从标号小的点走到大的点,每个人有一个走的距离的限制,问最后能拿到的最高的分数,当然这个人从(0 , 0)出发还得回到( 0 ...

  5. Androidclient和server端数据交互的第一种方法

    网上有非常多样例来演示Android客户端和server端数据怎样实现交互只是这些样例大多比較繁杂,对于刚開始学习的人来说这是不利的.如今介绍几种代码简单.逻辑清晰的交互样例,本篇博客介绍第一种: 一 ...

  6. A Game of Thrones(6) - Catelyn

    Of all the rooms in Winterfell’s Great Keep, Catelyn’s bedchambers(['bedtʃeɪmbə]卧室,寝室) were the hott ...

  7. hdu3899(树形dp)

    题意:给一树,每个结点有人数,边有权值,表示经过这条边所需时间, 问取某个结点作为开会地点,所有人全部到达此结点最少所需总时间? 分析:val[u]表示以u为根节点的总人数,num[u]表示以u为根节 ...

  8. Mit 分布式系统导论,Distributed Systems ,lab1 -lab6 总结,实验一到实验六总结

    终于把Mit的分布式系统导论课的实验1-6写完了 做得有些痛苦,但是收获也很大 http://pdos.csail.mit.edu/6.824-2012/labs/index.html 把实验1-6用 ...

  9. deinstall oracle 11g on linux

    deinstall oracle 11g on linux   From 11gR2, oracle provide us an deinstall tool. With that now we ca ...

  10. Windows Phone开发(13):如何规范用户的输入行为

    原文:Windows Phone开发(13):如何规范用户的输入行为 很多时候,我们对用户的操作或输入做一定程度的限制,以避免发生不必要的异常或错误,因此,对一些特殊的类型,进行输入限制是很有必要的. ...