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 ...
随机推荐
- UIWebView 使用要注意的几点
UIWebView 使用要注意的几点 最近有客户希望将移动端统一使用HTML5来完成,在iOS端就要用到UIWebView.遇到了以下三个主要问题: 加载HTTPS页面 不像Safari可以弹出弹框问 ...
- CodeForces 682C Alyona and the Tree(广搜 + 技巧)
方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...
- Django -- Views and URLconf
1 创建工程 django-admin startproject mysite && cd mysite 2 创建应用 python manage.py startapp blog 3 ...
- PAT (Advanced Level) 1094. The Largest Generation (25)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- mysql分页pagination
http://www.phpjabbers.com/php--mysql-select-data-and-split-on-pages-php25.html returns 20 records so ...
- 不停止MySQL服务增加从库的两种方式【转载】
现在生产环境MySQL数据库是一主一从,由于业务量访问不断增大,故再增加一台从库.前提是不能影响线上业务使用,也就是说不能重启MySQL服务,为了避免出现其他情况,选择在网站访问量低峰期时间段操作. ...
- 剑指offer 连续子序列和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public: int FindGreatestSumOfSu ...
- MediaScanner与音乐信息扫描==
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=98713 =================================== ...
- mr本地运行的几种模式
MR程序的几种提交运行模式 本地模型运行 1/在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行 ----输入输出数据可以放在本地路 ...
- web容器启动顺序
web容器启动顺序: 第一:context-param 第二:Listerer 第三:Filter 第四:servlet