Java实现-重排链表
来源:互联网 发布:流量软件哪个好 编辑:程序博客网 时间:2024/05/18 20:50
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The head of linked list. * @return: void */ public void reorderList(ListNode head) { // write your code here if(head==null){return;}int count=1;ListNode node1=head;while(node1.next!=null){node1=node1.next;count++;} ListNode node=head; ListNode result=head; Queue<ListNode> q=new LinkedList<ListNode>(); Stack<ListNode> s=new Stack<ListNode>(); if(count%2==0){ int num=count/2; while(num>0){ ListNode temp=node.next; node.next=null; q.add(node); node=temp; num--; } while(node!=null){ ListNode temp=node.next; node.next=null; s.push(node); node=temp; } q.poll(); while(!s.isEmpty()){ ListNode temp=s.pop(); result.next=temp; result=result.next; if(!q.isEmpty()){ temp=q.poll(); result.next=temp; result=result.next; }else{ break; } } }else{ int num=count/2+1; while(num>0){ ListNode temp=node.next; node.next=null; q.add(node); node=temp; num--; } while(node!=null){ ListNode temp=node.next; node.next=null; s.push(node); node=temp; } q.poll(); while(!s.isEmpty()){ ListNode temp=s.pop(); result.next=temp; result=result.next; if(!q.isEmpty()){ temp=q.poll(); result.next=temp; result=result.next; } } } }}
阅读全文
0 0
- Java实现-重排链表
- 重排链表
- 重排链表
- 重排链表
- 重排链表
- LintCode:重排链表
- LintCode : 重排链表
- 重排链表
- 重排链表
- 重排链表-LintCode
- LeetCode-143. Reorder List (JAVA)重排链表
- Reorder List 重排链表
- 算法:链表的重排
- Reorder List 链表重排
- L2-022. 重排链表
- lintcode(99)重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- 链式栈的实现
- canvas动画圆形扩散
- Java+Selenium3基础篇5-第一个自动化测试脚本
- 关于Android中WebView的点滴
- (搬运).net起步 (MemberShip 的配置)
- Java实现-重排链表
- c#中的正则表达式的源码
- 0017_Letter Combinations of a Phone Number
- Hibernate框架自学笔记
- LeetCode-62. Unique Paths
- JAVA关键字package/import详解
- 线程安全和可重入函数
- 好程序员的考核指标
- Android NDK开发Crash错误定位