[Linked List]Reorder List

otal Accepted: 54991 Total Submissions: 252600 Difficulty: Medium

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→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.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head)
    {
        ListNode* cur     = head;
        ListNode* newHead = NULL;
        ListNode* next    = NULL;
        while(cur){
            next = cur->next;
            cur->next = newHead;
            newHead = cur;
            cur = next;
        }
        return newHead;
    }

    void reorderList(ListNode* head)
    {
        if(head==NULL || head->next==NULL){
            return;
        }
        ListNode* slow = head;
        ListNode* fast = head;
        ListNode* pre  = NULL;
        while(fast){
            pre = slow;
            slow = slow->next;
            fast->next ? fast = fast->next->next : fast = NULL;
        }
        pre->next = NULL;
        ListNode* head2 = reverseList(slow);

        ListNode* cur       = head;
        ListNode* cur_next  = NULL;
        ListNode* cur2      = head2;
        ListNode* cur2_next = NULL;
        while(cur2){
            cur_next  = cur->next;
            cur2_next = cur2->next;

            cur->next  = cur2;
            cur2->next = cur_next;

            cur  = cur_next;
            cur2 = cur2_next;
        }
    }
};

Next challenges: (M) Partition List (M) Convert Sorted List to Binary Search Tree (H) Copy List with Random Pointer

时间: 2024-10-20 01:43:57

[Linked List]Reorder List的相关文章

leetcode day5 -- Reorder List && Linked List Cycle II

1.  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}. 分析:翻转链表是很常见的题目,这种翻转是第一次见.一开始

[LeetCode]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}. 这道题是将一个给定的单链表按照某种规则进行重排序,要求是不可以简单地直接交换结点中的值. 思路

143. 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}. 题解: 这道题虽然看上去很复杂,但经过分析,可知实质上这道题就是

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}. 这是一道比较综合的链表题目.一开始拿到手足无措.慢慢分析了一下,其实做法无非分三步: 1.将链表分

leetcode143 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}. 1 /** 2 * Definition for singly-linked list. 3 *

]Leetcode]-[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}. 题目的意思就是,给定一个链表,从两头开始链接, 比如1-2-3-4-5-6,最开始取两头,组成1-

leetcode 【 Reorder List 】python 实现

题目: 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}. 代码: oj 测试通过 248 ms 1 # Definition for singly-

Reorder List leetcod

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}. 题目的意思是要求对一个链表进行重新排序,如上面所示将 L0→L1→-→Ln-1→Ln, 重新排

[Leetcode][JAVA] 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}. 比较容易思考且实现的一个思路是, 将链表从中心拆成两半,后一半全部反向连接,然后前一半和后一半一个