amazon o2 - reverse second half linked list
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的更多相关文章
- LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- Reverse a singly linked list
Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...
- [轉]Reverse a singly linked list
Reverse a singly linked list http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- Leetcode-206 Reverse Linked List
#206. Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...
- [LeetCode] Reverse Linked List
Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...
- 【leetcode】Reverse Linked List(easy)
Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...
- 【12_206】Reverse Linked List
本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...
随机推荐
- Windows下配置nginx+php(wnmp)
第一部分:准备工作.(系统:Windows 8.1) 1.首先是下载软件. NGINX-1.3.8官网下载:http://nginx.org/en/download.html PHP5.4.8版本 ...
- Entity Framework – (复数)Plural and (单数)Singular 表名Table names
By default, the Entity Framework will assume that all of the names of your tables in your database a ...
- scala getter and setter
package exp { object Main { def main(args: Array[String]): Unit = { B.name ="Fred"; printl ...
- redis配置
# Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写)## 1k => 1000 bytes# 1kb => ...
- 20140701立项 移植WatermarkLabelSys
开始移植WatermarkLabelSys,从一个版本中抽离出最原始的内核,不求完善,只求能运行.时间半个月. 顺利的话针对不同的后缀.进程开始添加规则细节,时间1个月. 在顺利的话,兼容性测试,完善 ...
- 【 2013 Multi-University Training Contest 5 】
HDU 4647 Another Graph Game 如果没有边的作用,显然轮流拿当前的最大值即可. 加上边的作用,将边权平均分给两个点,如果一个人选走一条边的两个点,就获得了边的权值:如果分别被两 ...
- Win8 传统桌面下无法上网的解决方法
Win8 很酷,就连出现的问题也都酷得不行~ 之前遇到的一些问题与解决方法,避免重新安装 1. Word 2013下, 输入法突然秀逗, 经常按了一个键后, 死循环输入该字符直到死机 解决方法: 进入 ...
- java获取当前时间前一周、前一月、前一年的时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calend ...
- 简单研究Loader笔记
2015-11-11 18:25:34 1. Loader是什么? /** * Static library support version of the framework's {@link and ...
- SVN相关
Eclipse SVN忽略一些文件夹:Windows -> Preferences -> Team -> Ignored Resources里点 “Add Pattern”,然后把 ...