public ListNode reverseBetween(ListNode head, int m, int n) {
  if(head==null) return head;
  ListNode slow = head;
  ListNode fast = head;

  //find middle
  while(fast.next!=null) {
    fast = fast.next;
    if(fast.next!=null) {
      fast = fast.next;
    }
    else {
      slow = slow.next;
    }
  }
  ListNode current = slow.next;
  ListNode halfFirst = current;
  if(current == null || current.next==null) {
    return head;
  }
  ListNode next = current.next;

  // reverse
  while(next!=null) {
    ListNode temp = current;
    current = next;
    next = next.next;
    current.next = temp;
  }

  //combine
  slow.next = current;
  halfFirst.next = null;
  return head;
}

amazon o2 - reverse second half linked list的更多相关文章

  1. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  2. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  3. [轉]Reverse a singly linked list

    Reverse a singly linked list  http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...

  4. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  5. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

  6. [LeetCode] Reverse Linked List

    Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...

  7. Java for LeetCode 206 Reverse Linked List

    Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...

  8. 【leetcode】Reverse Linked List(easy)

    Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...

  9. 【12_206】Reverse Linked List

    本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...

随机推荐

  1. PHP 的 __FILE__ 常量

    今天碰到了PHP的常量__FILE__的问题了. 在网上查了一下.总结了以下规律. dirname(__FILE___) 函数返回的是脚本所在在的路径. 比如文件 b.php 包含如下内容: < ...

  2. C# Out,Ref 学习总结

    C# Out,Ref 学习总结. ref是传递参数的地址,out是返回值,两者有一定的相同之处,不过也有不同点. 使用ref前必须对变量赋值,out不用. out的函数会清空变量,即使变量已经赋值也不 ...

  3. Python实践所遇问题记录

    1.在cmd中直接输入'python'提示:'python'不是内部或外部命令,也不是可运行的程序或批处理文件. 原因:没有为Python设置环境变量. 解法:控制面板->系统->高级系统 ...

  4. MHA+Atlas+mysql一主一从开启gtid安装配置与实验

    各节点架构 (说明:生产环境有两个节点可以组成一套完整集群,我是测试环境,因此对于manager以及atlas和binlog server都是单点,如果生产环境,相应的将manager以及atlas和 ...

  5. 自己不懂的SQL语句用法

    left  join:是SQL语言中的查询类型,即连接查询.它的全称为左外连接,是外连接的一种. 连接通常可以在select语句的from子句或where子句中建立,其语法格式为: select  c ...

  6. Sql语句复习

    1.两张表A,B 其中A表中字段1,字段2,字段3要迁移到B表中字段4,字段5,字段6,迁移后B表的字段7 全赋值为123: insert into B(字段4,字段5,字段6,字段7) select ...

  7. 深入了解 Scala 并发性

    2003 年,Herb Sutter 在他的文章 “The Free Lunch Is Over” 中揭露了行业中最不可告人的一个小秘密,他明确论证了处理器在速度上的发展已经走到了尽头,并且将由全新的 ...

  8. 手机客户端UI测试常见的测试点

    1.各种分辨率下,显示正常.现市场上主流的塞班V3系统手机为240*320.320*240.WM系统主要为240*320.320*480.Android系统主要为320*480,Iphone系统为32 ...

  9. python cmd下运行中文乱码 策略

    如我运行一个脚本,执行后输出中文看不懂 然后呢在文件头加上 from __future__ import unicode_literals 注意:需要去掉所有字符串前的 u, 这样所有字符串均默认为u ...

  10. Android studio 项目的layout的文件打开,preview 视图无法显示,提示“no sdk found...”可能原因?

    1.安装android studio后启动,引导新的下载的sdk文件夹,不要默认在c:\users\你的用户名\appdata...下的sdk文件夹. 2.如果已经默认的,重新在settings/pr ...