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. hdu_5179_beautiful number(数位DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 题意:给你一个范围,问你漂亮的数有多少个,漂亮的数的定义为 数位高的比数位低的大,并且 数位高的 ...

  2. Android Studio的使用(十一)--每次打开时选择项目,而不是直接进入上次项目

    1.打开的时候选择打开哪一个项目 2.需要在设置System Setting,不要勾选Reopen last project on startup项.

  3. javascript 手机号码正则表达式验证函数

    代码如下: function checkMobile(){     var sMobile = document.mobileform.mobile.value     if(!(/^1[3|4|5| ...

  4. JSP文件上传--FileUpload组件

    如果使用上传操作,并且没有使用框架之类,最好使用Smartupload,因为FileUpdate太难使用. 下载组件: fileupload包:http://commons.apache.org/pr ...

  5. PHP 命名空间以及自动加载(自动调用的函数,来include文件)

    这篇文章的目的是记录 1. php中的自动加载函数 __autoload(), 和 spl_autoload_register()函数, 2 .php中命名空间的使用. 一.当不使用命名空间的时候 a ...

  6. PHP的json_encode中文被转码的问题

    在php5.2中做json_encode的时候.中文会被unicode编码, php5.3加入了options参数, 5.4以后才加入JSON_UNESCAPED_UNICODE,这个参数,不需要做e ...

  7. Ubuntu系统如何卸载并安装新版本的jdk(permission denied问题)

    1.如果是自带的openjdk运行下面可以删除: sudo apt-get purge openjdk* 但是这种情况又不一定成功,有时候运行java -version发现还是可以运行的.证明删除不成 ...

  8. 实例:SSH结合Easyui实现Datagrid的新增功能和Validatebox的验证功能

    在我前面一篇分页的基础上,新增了添加功能和添加过程中的Ajax与Validate的验证功能.其他的功能在后面的博客写来,如果对您有帮助,敬请关注. 先看一下实现的效果: (1)点击添加学生信息按键后跳 ...

  9. Struts1.x教程:配置文件总结

    要想使用Struts,至少要依靠两个配置文件:web.xml和struts-config.xml.其中web.xml用来安装Struts框架.而struts-config.xml用来配置在Struts ...

  10. Cells Not Under Attack

    Cells Not Under Attack Vasya has the square chessboard of size n × n and m rooks. Initially the ches ...