zoukankan      html  css  js  c++  java
  • 143. Reorder List

    Given a singly linked list LL0→L1→…→Ln-1→Ln,
    reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

    You may not modify the values in the list's nodes, only nodes itself may be changed.

    Example 1:

    Given 1->2->3->4, reorder it to 1->4->2->3.

    Example 2:

    Given 1->2->3->4->5, reorder it to 1->5->2->4->3.

    first use two pointers (slow, fast) to find the middle node of the linked list;

    then reverse the second half of the linked list;

    then merge the two halves one by one.

    p.s. 注意合并两个linked list时候的处理

    time: O(n), space: O(1)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public void reorderList(ListNode head) {
            if(head == null || head.next == null) {
                return;
            }
            // find middle node
            ListNode fast = head, slow = head;
            while(fast.next != null && fast.next.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            // split into two halves, reverse the second half
            ListNode second = reverse(slow.next);
            slow.next = null;
            // merge
            merge(head, second);
        }
        
        private ListNode reverse(ListNode head) {
            if(head == null || head.next == null) {
                return head;
            }
            ListNode prev = null, cur = head;
            while(cur != null) {
                ListNode next = cur.next;
                cur.next = prev;
                prev = cur;
                cur = next;
            }
            return prev;
        }
        
        private void merge(ListNode p1, ListNode p2) {
            while(p1 != null && p2 != null) {
                ListNode n1 = p1.next;
                ListNode n2 = p2.next;
                p1.next = p2;
                p2.next = n1;
                p1 = n1;
                p2 = n2;
            }
        }
    }
  • 相关阅读:
    如何解压.bz2文件包
    Typecho中的gravatar头像无法加载
    整理的mysql优化内容
    PHP常用的一些正则表达式
    git log --stat常用命令
    Linux查找含有某字符串的所有文件
    linux下如何查看chm文件
    linux访问windows共享文件夹的方法
    typecho除了首页其他大部分网页404怎么办?
    Windows在当前目录打开cmd
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10177664.html
Copyright © 2011-2022 走看看