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;
            }
        }
    }
  • 相关阅读:
    ivew-admin 导入excel
    ivew Upload 上传时附带的额外参数
    工厂方法模式
    简单工厂模式
    webpack (1)
    商品格子
    合同签名
    展示图片数组
    使用egg.js和egg-sequelize连接mysql
    egg 连接mysql 在mysql 插入数据
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10177664.html
Copyright © 2011-2022 走看看