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 + ...
随机推荐
- Java 类的一般特征
1. 创建类的对象时的内存结构 用图来解释: 使用new 创建 a1 时,成员变量的值都是初始默认值. 然后显式的改变其属性的值. 创建a3 时,a3 是直接指向 a1, 即a3 = a1, 两个对象 ...
- [Unity3D]调用Android接口
简介 有一些手机功能,Unity没有提供相应的接口,例如震动,例如不锁屏,例如GPS,例如... 有太多的特殊功能Unity都没有提供接口,这时候,我们就需要通过使用Android原生的ADT编辑器去 ...
- table td的宽度详解
前言:一直总觉得td的宽度好难驾驭,但万事万物总是有规律的.就像亮剑说的:不用因为怕八路就敬而远之,应该靠上去,熟悉他们,了解他们. 正文: Table只有Table的宽度是可 ...
- poj2137 dp
//Accepted 228K 32MS //dp[k][i][j] 表示从1的k号节点到i的j号节点的最小花费 //dp[k][i][j]=min(dp[k][i-1][h]+cost) cost为 ...
- IIS使用问题
1.System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序:修改IIS应用程序池的高级设置将32位设置成true
- os.system和os.popen
使用os.popen调用test.sh的情况: python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执 ...
- Osmocom-BB中cell_log的多种使用姿势
转载留做备份,原文地址:http://92ez.com/?action=show&id=23342 翻阅osmocom-bb源码的时候注意到,在cell_log中有非常多我们从来没有注意到的功 ...
- River Crossing 简单的动态规划 .
第一行 t 表示有几组测试数据 . 每组测试数据的 第一行是 n, m . 然后 下面有n行数据 . 题意:有1个人和N只羊要过河.一个人单独过河花费的时间是M,每次带一只羊过河花费时 ...
- MySQL数据库系统概述
了解MySQL数据库管理系统,内容如下: 一.基于数据库的PHP项目 目前动态网站都是基于数据库,将网站内容使用数据库管理系统去管理 用户, 栏目, 图片, 文章, 评论都 ...
- 【LeetCode OJ】Word Break
Problem link: http://oj.leetcode.com/problems/word-break/ We solve this problem using Dynamic Progra ...