1 package leetcode;

 public class ReOrderList {
public void reorderList(ListNode head) {
if(head==null||head.next==null||head.next.next==null){ }else{
int l=numNode(head);
ListNode mid = new ListNode(-1);
mid=getMid(head);
ListNode next=mid.next;
ListNode po=reverse(next);
mid.next=null;
ListNode p=head;
while(po!=null){
ListNode po2=po.next;
po.next=p.next;
p.next=po;
p=p.next.next;
po=po2;
}
} }
public int numNode(ListNode head){
if(head==null){
return 0;
}
int i=0;
while(head!=null){
i++;
head=head.next;
}
return i;
}
public ListNode getMid(ListNode head){
ListNode p=head;
ListNode q=head;
ListNode pre=head;
while(q.next!=null&&q.next.next!=null){
pre=p;
p=p.next;
q=q.next.next;
}
return p;
}
public ListNode reverse(ListNode n){
ListNode h = new ListNode(-1);
ListNode q = n;
while(q!=null){
ListNode next=q.next;
q.next=h.next;
h.next=q;
q=next;
}
return h.next;
}
public static void main(String[] args){
ListNode a=new ListNode(1);
ListNode b=new ListNode(2);
ListNode c=new ListNode(3);
ListNode d=new ListNode(4);
a.next=b;
b.next=c;
c.next=d;
d.next=null;
ReOrderList s = new ReOrderList();
s.reorderList(a);
ListNode p=a;
while (p != null) {
System.out.println(p.val);
p = p.next;
}
} }

leetcode-005 reorder list的更多相关文章

  1. 【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 ...

  2. [Leetcode Week6]Reorder List

    Reorder List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/reorder-list/description/ Description G ...

  3. [LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序

    You have an array of `logs`.  Each log is a space delimited string of words. For each log, the first ...

  4. 【leetcode】Reorder List (middle)

    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 ...

  5. 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 ...

  6. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. 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 thi ...

  8. [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

    For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...

  9. 【LeetCode】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 ...

  10. LeetCode Solutions : Reorder List

    →-→Ln-1→Ln, reorder it to: L→Ln-2→- You must do this in-place without altering the nodes' values. Fo ...

随机推荐

  1. 命令行调试smali

    am start -D -n com.agilebits.onepassword/.activity.LoginActivity ps | grep passu0_a126 1107 217 5107 ...

  2. UVALive 2523 Machine Schedule(二分图求最大匹配数)

    题意:有两台机器,上面有多个工作区域,有多个任务,分别可以在两台机器的某一个区域上完成,两台机器一开始都在0区域上工作,每次更改区域,都会重新启动一次,让我们求出最小的重启次数. 思路:将两个区域连线 ...

  3. C++文件编程(文件流操作)

    给出了比较常见的文件操作,包括二进制文件操作.代码如下: #include<iostream> #include<cstdio> #include<cstring> ...

  4. 滑轮关节(b2PulleyJoint)

    package{ import Box2D.Collision.b2AABB; import Box2D.Collision.b2RayCastInput; import Box2D.Collisio ...

  5. Oracle数据库设计小细节

    1. 如果使用PowerDesigner此类工具,注意将工具的导出的SQL语句中对于表的双引号去掉. 2. 建表和建字段的时候,不同单词之间使用下划线分隔,比如 REC_ID 3. Oracle中数值 ...

  6. hdu_2844_Coins(多重背包)

    题目连接:hdu_2844_Coins 题意:给你n个硬币的价值和对应的数量,问你从1到m有那些数能组合出来 题解:如果我们将硬币的价值看成一个物品的容量和价值,那么对应1-m,如果dp[i]==i, ...

  7. 关于MyEclipse不停报错multiple problems have occurred 或者是内存不足 的解决办法

    这是因为 worksapace与svn代码不一样,要更新! 一更新就好了,困扰死我了,卧槽,搞了2个小时,难怪svn一提交就卡死人,原来还就是svn的问题,更新一下就行.

  8. xshell 图形化连接ubuntu

    原文: http://jingyan.baidu.com/article/d45ad148967fcd69552b80f6.html Xmanager4系列软件是一套非常好的liunx远程操作,尤其是 ...

  9. Can't create handler inside thread that has not called Looper.prepare() 终极解决方法

    非主线程中出现 在报错的方法前加Looper.prepare(); 方法末尾加Looper.loop(); 很简单吧

  10. input file图片上传预览

    两种方法,方法一: js代码: //头像上传预览 $("#up").change(function() { var $file = $(this); var fileObj = $ ...