摘要: Given a singly linked listL: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}.[解题思路]将队列从中间断开,分成两个队列p1,p2,p2反转,然后将p1,p2进行合并 1 public class Solution { 2 public void reorderList(ListNo 阅读全文
posted @ 2013-11-15 22:39 feiling 阅读(581) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?[解题思路]递归解法 1 public ArrayList preorderTraversal(TreeNode root) { 2 // IMPORTANT: ... 阅读全文
posted @ 2013-11-15 22:24 feiling 阅读(217) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?[解题思路]双指针问题 详见:链表 相交结点与环问题 1 public class Solution { 2 public ListNode detectCycle(ListNode head) { 3 // IMPORTANT: Please reset any member d... 阅读全文
posted @ 2013-11-15 16:12 feiling 阅读(212) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?[解题思路]双指针问题,fast指针每次走两步,slow指针每次走一步,如果fast指针可以"追上"slow指针,则说明存在环 1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 // IMPORTANT: Please reset any member data yo.. 阅读全文
posted @ 2013-11-15 16:00 feiling 阅读(205) 评论(0) 推荐(0) 编辑