Posts Tagged 【List 】Reorder List
来源:互联网 发布:mysql 主备热切换 编辑:程序博客网 时间:2024/04/30 04:33
Reorder List
Total Accepted: 37888 Total Submissions: 180454My SubmissionsGiven a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}
, reorder it to {1,4,2,3}
.
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ /* 我的思路比较简单,牺牲内存来处理,明天想想直接用指针来 建立一个headnew,方便处理 最后的next为null */public class Solution { public void reorderList(ListNode head) { if(head == null) return; List<ListNode> list = new ArrayList<ListNode>(); ListNode headNew = new ListNode(0); headNew.next = head; ListNode p = head; while(p!=null) { list.add(p); p = p.next; } int len = list.size(); p = headNew; for(int i = 0;i<len/2;i++) { p.next = list.get(i); p.next.next = list.get(len-1-i); p = p.next.next; } p.next = list.get(len/2); p.next.next = null; }}
</pre><pre code_snippet_id="651774" snippet_file_name="blog_20150423_1_2185049" name="code" class="java">/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ /* 接着来 将list分成前后两个部分 后一个部分反转 两个部分插入 注意将最后的node的next赋值为null */public class Solution { public void reorderList(ListNode head) { if(head == null || head.next == null) return; twistList(head,reverseList(midList(head))); } public ListNode midList(ListNode head) { if(head == null || head.next == null) return null; ListNode p = head,q = head.next; while(q != null && q.next != null) { p = p.next; q = q.next.next; } return p.next; } public ListNode reverseList(ListNode head) { ListNode p=head,q=head.next; head.next = null; while(q != null) { ListNode temp = q.next; q.next = p; p = q; q = temp; } return p; } public ListNode twistList(ListNode head1,ListNode head2) { ListNode q = head1,p = head2; ListNode newHead = new ListNode(0); ListNode qp = newHead; while(q != null) { if(p != null) { ListNode qTemp = q.next; ListNode pTemp = p.next; qp.next = q; qp.next.next = p; qp = p; q = qTemp; p = pTemp; } else { qp.next = q; qp.next.next = null; break; } } return newHead.next; }}
Have you met this question in a real interview?
0 0
- Posts Tagged 【List 】Reorder List
- Posts Tagged 【List】Anagrams
- Posts Tagged 【List】Remove Linked List Elements
- Posts Tagged 【list】Reverse Linked List
- Posts Tagged 【List】Swap Nodes in Pairs
- Posts Tagged 【hashMap && Double Linked List】LRU Cache
- Posts Tagged 【list && tree && dfs】Flatten Binary Tree to Linked List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- 快速幂取模
- Android4.4编译环境的搭建和编译
- UITableViewCell的动态计算
- Java异常处理
- 逆序打印链表
- Posts Tagged 【List 】Reorder List
- JSP小结
- FFMPEG.H264解码解析-转自雷神
- gcd 简单介绍
- Android 开发规范
- POJ2996Help Me with the Game
- Servlet小结
- spark:学习杂记--29
- CXF报安全性错误 Cannot create a secure XMLInputFactory