Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given ->->->->->NULL, m = and n = , return ->->->->->NULL.
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
assert(m >= && n >= && m <= n);
if(head == NULL) return head ; ListNode * headm, *p, *q,*preP,*endm; //find the mth node
int i = ;
p = head ;
preP = NULL;
while(i<m &&p){
preP = p;
p= p->next;
i++;
} //reverse Between m and n
headm = NULL;
endm = p;
while(i<=n && p)
{
q = p;
p = p->next;
q->next = headm;
headm = q;
i++;
} if(preP == NULL)
head = headm;
else
preP->next = headm ; if(endm)
endm->next = p; return head;
}
};

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

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

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

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

  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. [Linked List]Reverse Linked List,Reverse Linked List II

    一.Reverse Linked List  (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...

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

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

  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. logstash 发送慢页面到zabbix告警

    input { file { type => "zj_frontend_access" path => ["/data01/applog_backup/zjz ...

  2. 【转】GitHub问题之恢复本地被删除的文件

    原文网址:http://blog.csdn.net/iaiti/article/details/39557951 折腾了真久,GitHub commit之后,我手痒把本地的一个文件给删了,然后一直gi ...

  3. cf445A DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. 旋的X-Di

    旋的X-Di | 氪加 旋的X-Di

  5. HTML--鼠标事件

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. python数据类型—列表(增改删查,统计,取值,排序)

    列表是最常用的数据类型之一,通过列表可以对数据实现方便的存储,修改等操作. 先声明一个空列表: >>> names = [] >>> names [] 可以存多个值 ...

  7. JavaScript 克隆对象

    function clone(origin) { return Object.assign({}, origin); } let aClone = { ...a }; // 等同于 let aClon ...

  8. socket用法以及tomcat静态动态页面的加载

    一.套接字的使用: 分为以下几步: 1.创建ServerSocket 2.接收客户端的连接 3.读取本地的test.html文件 4.构建数据输出通道 5.发送数据 6.关闭资源 代码参考: pack ...

  9. [RxJS] Basic DOM Rendering with Subscribe

    While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscri ...

  10. 用递归翻转一个栈 Reverse a stack using recursion

    明白递归语句之前的语句都是顺序运行,而递归语句之后的语句都是逆序运行 package recursion; import java.util.Stack; public class Reverse_a ...