leetcode-005 reorder list
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的更多相关文章
- 【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 Week6]Reorder List
Reorder List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/reorder-list/description/ Description G ...
- [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 ...
- 【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 ...
- 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 ...
- 【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 ...
- 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 ...
- [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 ...
- 【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 ...
- 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 ...
随机推荐
- struts2语法--error页面如何捕获?
如果地址栏输入了不带后缀或者action为后缀, 不存在的页面跳转到error.jsp: struts.xml配置" <package name="default" ...
- jsp内部传参与重定向传参
1 重定向地址栏会发生改变,因为它会发送两次请求,内部转发,地址栏不会发生改变,因为它只有一个请求2 重定向不能获取上一次请求中的参数,而内部转换可以3 内部转发可以访问WEB-INF下的资源,重定向 ...
- Net 自定义Excel模板导出数据
转载自:http://www.cnblogs.com/jbps/p/3549671.html?utm_source=tuicool&utm_medium=referral 1 using Sy ...
- aspx 文件上传和下载,多文件上传
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiFileUpload. ...
- Naive Bayes在mapreduce上的实现(转)
Naive Bayes在mapreduce上的实现 原文地址 http://www.cnblogs.com/sunrye/p/4553732.html Naive Bayes是比较常用的分类器,因为思 ...
- HTML中为何P标签内不可包含DIV标签? (转)
起因:在做项目时发现原本在DW中无误的代码到了MyEclipse6.0里面却提示N多错误,甚是诧异.于是究其原因,发现块级元素P内是不能嵌套DIV的. 深究:我们先来认识in-line内联元素和blo ...
- hdu1915
对于图的东西总是一筹莫展,没办法,还是翻出以前的基础题来看看,然后慢慢分析吧.路漫漫其修远兮,吾将上下而求索…… void bfs(int x,int y){ for(int k=0;k<8;k ...
- unbtun python tab补全
在使用python的时候有时候总是忘记很多代码,这个是作为程序袁最头疼的事情,本人也是刚刚接触python,这几天也是用到这块,所以记录下来,已被需要时能够找到. 我的系统是: w@w:~$ una ...
- C#+QI的例子
COM中,和我们打交道的是接口,也就是说类对我们是隐形的,那么我们要做开发,要使用这些功能,我们只能通过接口,通过接口暴露出来的方法,COM是一种服务器端/客户端架构,服务器端定义了操作的法,客户端通 ...
- 猜数游戏-flag的运用
package my;import java.util.Scanner;public class MyJava { public static void main(String[] ar ...