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 ...
随机推荐
- Gerald's Hexagon
Gerald's Hexagon Gerald got a very curious hexagon for his birthday. The boy found out that all the ...
- <c:forEach>循环list,一个表格两列数据
参考: http://zhidao.baidu.com/link?url=apG5dUmW7RjB5eOYKSWOWdKd7nxFpkDO4n3i8R6MWYKl7E2JC1OCtPILF4G4EUO ...
- visio个人专注
字体颜色 1 填充 2 标注 3 箭头 4 线条 5 粗细 6
- 最近在做外贸网站的时候,需要大量的字体来充实页面,就学习了怎么引用Google Fonts
第一步,FQ进入谷歌官方字体网站:https://fonts.google.com 妥妥的. 第二步,点击你所选择字体演示块的右上角的加号,然后你所选择的字体会形成引用链接以及你所要写的css样式. ...
- javascript无线端的判断
经常在工作中要求网页做这样的处理判断:针对无线端的网页,我们需要兼容一种异于PC端的网页效果. 如下代码就可以实现无线端的判断: var mobs = ['android', 'ipad', 'ipo ...
- C——malloc & free(转载自bccn C语言论坛)
原帖及讨论:http://bbs.bccn.net/thread-82212-1-1.html 原文链接:http://www.bccn.net/Article/kfyy/cyy/jszl/20060 ...
- EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(完)
前言 这一篇是本系列的最后一篇,虽然示例讲到这里就停止呢,但对于这些技术的学习远不能停止.虽然本示例讲的比较基础,但是正如我第一篇说到的,这个系列的目的不是说一些高端的架构设计,而是作为一个入门级,对 ...
- 【接口】【USB】1.学习笔记
1.USB的优点: 可以热插拔,即插上后可以自动识别: 系统总线供电,USB共有四根线,一根电源线,一根地线,一根D+线,一根D-线,D+和D-线是差分输入线: 可以支持多种设备,且扩展容易,通过HU ...
- Visual Basic 2012 借助DataGridView控件将SQL server2012 数据导入到Excel 2010
摘 要: SQL Server 2012 数据和Excel 2010之间的连接和数据的传输,本篇文章主要针对的是SQL Server 2012 数据导入到Excel 2010文件中.Excel软件对 ...
- .NET C# 将 mdb 中数据读为 list<string[]> 其中 path 为数据库地址 ,sql 为查询语句
using System.Data; using System.Data.OleDb; public static List<string[]> select_list(string pa ...