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 ...
随机推荐
- CentOS_7.2服务器前期
一.禁用SELinux:# 永久禁用,需要重启生效: sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux se ...
- outline使用方法,outline与border的区别:
在浏览器里,当鼠标点击或使用Tab键让一个链接或者一个radio获得焦点的时候,该元素将会被一个轮廓虚线框围绕.这个轮廓虚线框就是 outline . outline 能告诉用户那一个可以激发事件的h ...
- 微博开放平台开发(一)获取access_token
因为工作需要,接触到微博开放平台开发.特做此记录方便查用. 一.准备. 1.微博账号.注册很容易. 2.微博账号成为开发者. 登录微博开放平台 登录你注册的账号,然后进入管理中心完善开发者基本信息和 ...
- 使用VC6.0创建和运行C程序的方法
使用VC6.0可以有快捷的方式创建C程序,对于初学者,建议按照如下方式创建.先创建一个工作区,然后创建工程,最后在工程中创建源文件文件.理解工作区.工程与文件之间的关系.
- 吐槽!important专用博文
在IT公司实习了1个多月,氛围还是不错的,也算是积累了一些项目经验,同时在代码模块化.版本控制.任务优先级等方面有了更进一步的体会和理解,深刻认识到在一个团队,最重要的是沟通和负责. 嗯,说了下题外话 ...
- C#获取实体类属性名称
方法: public static string GetPropertyName(Expression<Func<SupplierInfos, string>> expr) { ...
- 关于opacity的兼容问题
各个浏览器的解决办法: css: .opacity{ filter:alpha(opacity=50); /* IE */ -moz-opacity:0.5; /* 老版Mozilla */ -kht ...
- Unity3D之GUITexture的坐标体系
Unity3D的GUITexture的坐标,其中x和y的取值在0~1之间,层次使用z来划分,值越大越靠前.
- 黑马程序员——C语言基础 内存剖析
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)进制 进制是一种计数的方式,数值的表示形式.有多种进制十进制.二进制 ...
- asp.net文件下载文件另存为
这是一个困惑已久的问题…… 首先,用<a>标签的href打开浏览器能解读的文件(如txt,jpg,pdf等),会自动打开,无法做到弹出另存为的效果. 其次,网上搜索了各种JS解决办法,包括 ...