Reorder List -- leetcode

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上实际执行时间为72ms。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        if (!head || !head->next)
            return;

        ListNode *walker = head;
        ListNode *runner = head->next;
        while (runner && runner->next) {
            walker = walker->next;
            runner = runner->next;
            runner = runner->next;
        }

        ListNode middle(0);
        runner = walker;
        walker = walker->next;
        runner->next = NULL;
        while (walker) {
            ListNode *bak = walker->next;
            walker->next = middle.next;
            middle.next = walker;
            walker = bak;
        }

        while (middle.next) {
            ListNode *bak = middle.next->next;
            middle.next->next = head->next;
            head->next = middle.next;
            head = head->next->next;
            middle.next = bak;
        }
    }
};
时间: 2024-10-14 01:58:00

Reorder List -- leetcode的相关文章

Reorder List leetcode java

题目: 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→Ln→L1→Ln-1→L2→Ln-2→…来排列,看例子1

143. Reorder List - LeetCode

Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前后两部分,将后部分链表反转,再将两部分链表连接成一个新链表 Java实现: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x)

Reorder List [leetcode] 这两种思路

第一个想法随着vector保存全部Node* 表拼接出来 void reorderList(ListNode *head) { vector<ListNode*> content; ListNode * cur = head; while (cur) { content.push_back(cur); cur = cur->next; } int size = content.size(); cur = NULL; for (int i = 0; i <= size - 1 - i

Reorder List [leetcode] 的两种思路

第一种思路是用一个vector存所有的Node* 之后再用两个指针将链表拼接出来 void reorderList(ListNode *head) { vector<ListNode*> content; ListNode * cur = head; while (cur) { content.push_back(cur); cur = cur->next; } int size = content.size(); cur = NULL; for (int i = 0; i <=

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

[leetcode]Reorder List @ Python

原题地址:http://oj.leetcode.com/problems/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,

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}. 原题链接:https://oj.leetcode.com/problems/reorder-l

LeetCode: Reorder List [143]

[题目] 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

LeetCode解题报告:Reorder List

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.针对第二个子链表求倒序