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.

Summary: First writes down the reverse parts (m to n) , then handles nodes before the m-th node and after the n-th node.

 class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode * current = head;
ListNode * pre_m_node = NULL;
ListNode * new_head = NULL;
int i = ;
while(current != NULL) {
if(i == m -)
break;
pre_m_node = current;
i ++;
current = current -> next;
}
//reverse m to n
ListNode * pre_n_node = current;
int num_of_reverse_op = ;
int num_of_nodes_to_reverse = n - m + ;
while(num_of_reverse_op < num_of_nodes_to_reverse) {
ListNode * pre_head = new_head;
new_head = current;
current = current -> next;
num_of_reverse_op ++;
new_head -> next = pre_head;
}
//connect rest node after the nth node
pre_n_node -> next = current; if(pre_m_node != NULL) {
pre_m_node -> next = new_head;
return head;
} else {
return new_head;
}
}
};

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

  1. Reverse Linked List II——LeetCode

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

  2. Reverse Linked List II leetcode java

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

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

  4. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

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

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

  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-pass. F ...

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

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

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

随机推荐

  1. jquery之each()、$.each()[jQuery.each()]

    导航: 1,jQuery对象(实例)的each()函数 2,全局jQuery对象的each()函数 一:jQuery对象(实例)的each()函数 each()函数用于以当前jQuery对象匹配到的每 ...

  2. 安装 SQLManagementStudio_x86_CHS(SQL Server Management Studio) 老提示重启的解决办法

    安装 SQL Server Management Studio(SQLManagementStudio_x86_CHS)时,检测时不通过,提示重启电脑,我以为她安装了什么心软件没有重启:所以重启了电脑 ...

  3. Java中正则表达式的使用

    public class Test{ public static void main(String args[]) { String str="@Shang Hai Hong Qiao Fe ...

  4. python内置函数的归集

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...

  5. C#中实现多继承的方法

    有一个类叫老虎,还有一个类叫苍蝇.现在新创一个超级老虎类,一种可以飞的老虎,超级老虎由于同时也继承自苍蝇 namespace Interface { //飞的接口 public interface I ...

  6. 详解.NET异步

    在说到异步前,先来理一下几个容易混淆的概念,并行.多线程.异步. 并行,一般指并行计算,是说同一时刻有多条指令同时被执行,这些指令可能执行于同一CPU的多核上,或者多个CPU上,或者多个物理主机甚至多 ...

  7. 坐标随鼠标移动 jquery简易版

    <html> <span style="position:absolute" id="xy_test"></span> &l ...

  8. iOS - OC NSUserDefaults 数据存储

    前言 @interface NSUserDefaults : NSObject 用来保存应用程序设置和属性.用户保存的数据.用户再次打开程序或开机后这些数据仍然存在.如果往 userDefaults ...

  9. SAP 批量查看凭证更改记录

    1,在凭证上点击环境->凭证变更 查找.2,通过运行程序 SE38:RSSCD1TS 根据对象类.对象标识查找. 3,SE16N/SE16/SE11查看标准表,CDHDR(更改凭证抬头),CDP ...

  10. retire or not retire ? is a question.

    corejava 上的一段代码 因吹思婷 "C:\Program Files\Java\jdk1.8.0_101\bin\java" -Didea.launcher.port=75 ...