【LeeCode】Reorder List 重排链表

来源:互联网 发布:淘宝代运营 编辑:程序博客网 时间:2024/06/05 18:39

Reorder List 重排链表
Given 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}.

给定一个单链表L: L0→L1→…→Ln-1→Ln,
重新排列后为:L0→Ln→L1→Ln-1→L2→Ln-2→…

必须在不改变节点值的情况下进行原地操作。

样例
给出链表 1->2->3->4->null,重新排列后为1->4->2->3->null。

挑战
Can you do this in-place without altering the nodes’ values?

(1)Java版

/*** 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: nothing    */    private ListNode reverse(ListNode head) {        ListNode prev = null;        while (head != null) {            ListNode temp = head.next;            head.next = prev;            prev = head;            head = temp;        }        return prev;    }    private void merge(ListNode head1, ListNode head2) {        // if(head1 == null || head2 == null) return;        int index = 0;//以后续判断奇偶序数。        ListNode dummy = new ListNode(0);        while (head1 != null && head2 != null) {            if (index % 2 == 0) {                dummy.next = head1;                head1 = head1.next;            }            else {                dummy.next = head2;                head2 = head2.next;            }            dummy = dummy.next;            index++;        }        // ListNode h3 = (head1 != null)? head1 : head2;        // dummy.next = h3;        if (head1 != null) {            dummy.next = head1;        }        else {            dummy.next = head2;        }    }    private ListNode findMiddle(ListNode head) {        ListNode slow = head, fast = head.next;        while (fast != null && fast.next != null) {            slow = slow.next;            fast = fast.next.next;        }        return slow;    }    public void reorderList(ListNode head) {        if (head == null || head.next == null) return;        ListNode mid = findMiddle(head);//招中点(via 快慢指针)以安全递归        ListNode tail = reverse(mid.next);//将链表后半截逆置。        mid.next = null;//将head链表从中间断开。(只保留了前半截)        merge(head, tail);//递归    }}

(2)C++版

/** * Definition of ListNode * class ListNode { * public: *     int val; *     ListNode *next; *     ListNode(int val) { *         this->val = val; *         this->next = NULL; *     } * } */class Solution {public:    /**     * @param head: The first node of linked list.     * @return: void     */    void reorderList(ListNode *head) {        // write your code here        if (head == NULL)            return;        vector<ListNode*> nodes;        ListNode* iter = head;        while(iter != NULL)        {            nodes.push_back(iter);            iter = iter->next;        }        int LEN = nodes.size();        int left = 0;        int right = LEN -1;        while(left < right)        {            nodes[left]->next = nodes[right];            nodes[right--]->next = nodes[++left];        }        nodes[left]->next = NULL;    }};
原创粉丝点击