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 ...
随机推荐
- gulp相关知识(2)
将之前的东西的理论部分整理了一下—— gulp 前端构建工具 它可以帮助我们完成一些自动化操作 它需要一些插件的支持 var gulp = require('gulp'); --> gulp命令 ...
- excel转化为Json
Sheet sheet; Workbook book; Cell cell1,cell2,cell3,cell4; JSONArray jsonArray = ...
- 常见的http状态码
[200]请求已成功,请求所希望的响应头或数据体将随此响应返回. [301]被请求的资源已永久移动到新位置,并且将来任何对此资源的引用都应该使用本响应返回的若干个URI之一.如果可能,拥有连结编辑功能 ...
- android:editable is deprecated: Use an <EditText> to make it editable
问题:android:editable is deprecated: Use an to make it editable 意思:Android的:编辑是反对:使用<</span> ...
- mysql笔记7之数据类型
1 区别一: varchar:可变长度的字符串.根据添加的数据长度决定占用的字符数 char:固定长度的字符串 2区别二 int:没有限制 int(4):限制为4 3 区别三: 日期: date ...
- odd or even?
public boolean isEven(int data){ if((data&1)== 0) return true; return false; } much faster than ...
- WebClient 访问间歇性返回403解决方案
说明:前段时间做的一个项目莫名的返回403的错误,这种情况也多大是程序员最不喜欢的了,没办法先来分析一下错误信息.之前的代码如下: WebClient webclient = new WebClien ...
- PHP个人常用函数封装
function GetIP(){ if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $cip = $_SERVER["HTTP_CLIEN ...
- 【嵌入式linux】(第三步):安装串口终端 (ubuntu安装minicom串口终端)
1.前言 我使用的是USB转串口,芯片是PL2303,貌似ubuntu自带了PL2303的USB驱动,可以直接使用,其它的USB转串口的没试过. 2.minicom安装 在终端中输入 : sudo a ...
- 关于svn的安装问题
直接把安装包放在 MyEclipse的安装目录下 如果 报错 那么就重装MyEclipse 半个小时左右就OK了不然调错太浪费时间了