[leetcode]Reorder List
来源:互联网 发布:淘宝助手 mac 编辑:程序博客网 时间:2024/05/29 12:02
public class Solution {
public static void reorderList(ListNode head) {
if(head==null||head.next==null)
return ;
ListNode tmp,revhead=head,pre,cur=head,next;
int len=0,lp;
while(cur!=null){
cur=cur.next;
len++;
}
lp=(len+1)/2;
cur=head;
while(lp-->1){
cur=cur.next;
}
revhead=cur.next;
cur.next=null;
cur=revhead.next;
pre=revhead;
while(cur!=null){
next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
revhead.next=null;
//反转之后的链表头
revhead=pre;
cur=head;
while(revhead!=null){
next=cur.next;
tmp=revhead.next;
cur.next=revhead;
revhead.next=next;
cur=next;
revhead=tmp;
}
return ;
}
public static void main(String[] args) {
ListNode t1=new ListNode(1);
ListNode t2=new ListNode(2);
ListNode t3=new ListNode(3);
t1.next=t2;
t2.next=t3;
reorderList(t1);
}
}
public static void reorderList(ListNode head) {
if(head==null||head.next==null)
return ;
ListNode tmp,revhead=head,pre,cur=head,next;
int len=0,lp;
while(cur!=null){
cur=cur.next;
len++;
}
lp=(len+1)/2;
cur=head;
while(lp-->1){
cur=cur.next;
}
revhead=cur.next;
cur.next=null;
cur=revhead.next;
pre=revhead;
while(cur!=null){
next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
revhead.next=null;
//反转之后的链表头
revhead=pre;
cur=head;
while(revhead!=null){
next=cur.next;
tmp=revhead.next;
cur.next=revhead;
revhead.next=next;
cur=next;
revhead=tmp;
}
return ;
}
public static void main(String[] args) {
ListNode t1=new ListNode(1);
ListNode t2=new ListNode(2);
ListNode t3=new ListNode(3);
t1.next=t2;
t2.next=t3;
reorderList(t1);
}
}
0 0
- [leetcode][list] Reorder List
- [leetcode][list] Reorder List
- Leetcode: Reorder List
- [LeetCode] Reorder List
- LeetCode题解:Reorder List
- Leetcode: Reorder List
- LeetCode:Reorder List
- leetcode之Reorder List
- Leetcode: Reorder List
- [LeetCode]Reorder List
- [LeetCode] Reorder List
- leetcode Reorder List
- LeetCode | Reorder List
- LeetCode Reorder List
- LeetCode 之 Reorder List
- Leetcode: Reorder List
- LeetCode OJ:Reorder List
- Reorder List (LeetCode)
- Android自定义控件初步之基本知识
- OSI七层与TCP/IP五层网络架构详解
- SparkContext类和SparkConf类
- HDU 5504 GT and sequence 模拟
- Ajax详解
- [leetcode]Reorder List
- 内存管理器(五)Glibc malloc实现(一)(概论)
- 当在浏览器地址栏输入一个URL后回车,将会发生的事情?
- [Object C]_[初级]_[NSArray排序]
- 012.季节判断
- [开源与硬件]USB模块的驱动开发要吃透
- RDD的创建
- Web 服务编程,REST 与 SOAP
- 棋牌游戏发展前景分析