99
来源:互联网 发布:2017八月经济数据 编辑:程序博客网 时间:2024/04/23 20:39
2017.10.23
首先遍历链表,建立两个队列,一个存放前一半一个存放后一半。
然后再遍历这两个队列,建立新的连接关系,重排链表。
/** * 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){if(head == null || head.next == null){return ;}LinkedList<ListNode> queue1 = new LinkedList<ListNode>();LinkedList<ListNode> queue2 = new LinkedList<ListNode>();//所有的节点均先入栈while(head != null){queue1.addFirst(head);head = head.next;}int i = queue1.size()/2;while(i > 0){queue2.addLast(queue1.pollFirst());i--;}head = queue1.pollLast();head.next = null;ListNode bt = head;while(!queue1.isEmpty()){queue2.peekFirst().next = queue1.peekLast();queue1.peekLast().next = bt.next;bt.next = queue2.pollFirst();bt = queue1.pollLast();}while(!queue2.isEmpty()){queue2.peekFirst().next = bt.next;bt.next = queue2.pollFirst();}}}
阅读全文
0 0
- 99
- 99
- 99
- 99
- 99
- 9+99+99+......+9999999
- 99布板
- 99房
- apple 99¥
- 99乘法
- 99法则
- 99乘法
- 99表
- ocp-99
- 99累加
- 99累加
- 99累加
- 99累减
- #for---else的应用
- MySQL常用DDL
- 互联网金融大佬频出,贷款买车行业为何难觅独角兽
- C++基础二
- Cocoapod方式引入百川SDK -报错[!] Unable to find a specification for `xxx`
- 99
- 第五章 Qt双缓冲(double buffering)
- MySQL和Oracle使用group by的区别
- java_se_recall 看程序写结果测试题 (包含继承 重写 多态知识)
- Codeforces 100548G
- ubuntu 12.04安装telnet和ssh服务
- xml && 注解——>配置事务管理
- cocos2dx的ActionTween 动作使用方法
- AndFix使用总结