leetcode 143. Reorder List ----- java
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}
, reorder it to {1,4,2,3}
.
按照题意改变链表结构。
1、使用list记录链表。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) { List<ListNode> list = new ArrayList<ListNode>();
ListNode node = head;
while( node != null ){
list.add(node);
node = node.next;
}
int len = list.size();
if( len < 3)
return ; node = head;
node.next = list.get(len-1);
node = node.next;
for( int i = 1;i<len/2;i++){ node.next = list.get(i);
node.next.next = list.get(len-1-i);
node = node.next.next;
}
if( len%2 != 0){
node.next = list.get(len/2);
node = node.next;
}
node.next = null; return ;
}
}
2、使用双向队列。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) { Deque<ListNode> deque = new ArrayDeque<ListNode>(); ListNode node = head; while( node != null ){
deque.add( node );
node = node.next;
}
if( deque.size() < 3)
return ;
node = deque.poll();
node.next = deque.pollLast();
node = node.next;
while( !deque.isEmpty() ){
node.next = deque.poll();
node.next.next = deque.pollLast();
node = node.next.next;
}
if( node != null )
node.next = null;
return ;
}
}
3、不使用额外空间,找到中间点,然后将后半部分链表反转,然后将两个链表合并即可。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) { if( head == null || head.next == null || head.next.next == null )
return ;
ListNode slow = head;
ListNode fast = head.next;
while( fast!= null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
} ListNode node2 = slow;
fast = slow.next; while( fast != null ){ ListNode node = fast.next;
fast.next = slow;
slow = fast;
fast = node; }
node2.next = null;
node2 = slow;
ListNode node1 = head; while( node1 != null ){
ListNode node = node1.next;
ListNode nn = node2.next;
node1.next = node2;
node2.next = node;
node1 = node;
node2 = nn;
} return ;
}
}
leetcode 143. Reorder List ----- java的更多相关文章
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- Java for LeetCode 143 Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...
- 【Leetcode】Reorder List JAVA
一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must ...
- Leetcode 143. Reorder List(Medium)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- [leetcode]143. Reorder List重排链表
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...
- Leetcode#143 Reorder List
原题地址 先把链表分割成前后两半,然后交叉融合 实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量.功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错. 代码: ListNode ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 143. Reorder List - LeetCode
Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
随机推荐
- [转载]Android 异步加载解决方案
2013-12-25 11:15:47 Android 异步加载解决方案,转载自: http://www.open-open.com/lib/view/open1345017746897.html 请 ...
- NOIP 2013 提高组 day1 T2 火柴排队 归并 逆序对
描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑i=1n(ai−bi)2∑i=1n(ai−bi) ...
- WP8.1 Study12:文件压缩与Known Folder(包含SD卡操作)
一.文件压缩 当应用程序保存和加载数据,它可以使用压缩. 1.使用 Windows.Storage.Compression.Compressor 压缩,获得一个Compressor stream. v ...
- string.format
string.Format("{0:#,0}", c.num), //千分号,有小数就保留2位小数 string.Format("{0:N2}", c.amou ...
- [转]powerDesigner生成excel版本的数据库文件
powerDesigner生成excel版本的数据库文件 出处:http://ray-allen.iteye.com/blog/1893347 脚本 excel 今天收到一个需求,要把数据库设计给一 ...
- HTTP状态代码
1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101 (切换协议) 请求 ...
- [转]Putty中文乱码解决方法
from: http://www.putty.ws/putty-luanma putty使用的过程中,你是否遇到过putty出现中文乱码的情况呢?putty终端出现乱码,是情况,多数是由于系统或软件缺 ...
- sql游标的使用入门
游标的理解: 游标其实可以理解成一个定义在特定数据集上的指针,我们可以控制这个指针遍历数据集,或者仅仅是指向特定的行,所以游标是定义在以Select开始的数据集上的 普通的sql语句是面向集合的,游标 ...
- keychain 中的概念理解
kSecAttrAccessible 这个属性控制Keychain中的一个Item什么时候可以被访问,可选值有:kSecAttrAccessibleWhenUnlocked, kSecAttrAcce ...
- OBJECT ARX 添加标注样式
////获得当前图形的标注样式表 AcDbDimStyleTable* pDimStyleTbl; acdbHostApplicationServices()->workingDatabase( ...