143. Reorder List(List)

Given a singly linked list L: L0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-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}.

class Solution {
public:
    void reorderList(ListNode *head) {
        if (!head|| !head->next)
        return;  

        // partition the list into 2 sublists of equal length
        ListNode *slowNode = head, *fastNode = head;
        while (fastNode->next) {
            fastNode = fastNode->next;
            if (fastNode->next) {
                fastNode = fastNode->next;
            } else {
                break;
            }
            slowNode = slowNode->next;
        }
        // 2 sublist heads
        ListNode *head1 = head, *head2 = slowNode->next;
        // detach the two sublists
        slowNode->next = NULL;  

        // reverse the second sublist
        ListNode *cur = head2, *post = cur->next;
        cur->next = NULL;
        while (post) {
            ListNode* temp = post->next;
            post->next = cur;
            cur = post;
            post = temp;
        }
        head2 = cur; // the new head of the reversed sublist  

        // merge the 2 sublists as required
        ListNode* p = head1, *q = head2;
        while (q ) {
            ListNode *temp1 = p->next;
            ListNode *temp2 = q->next;
            p->next = q;
            q->next = temp1;
            p = temp1;
            q = temp2;
        }
    }
};
时间: 2024-10-15 16:49:35

143. Reorder List(List)的相关文章

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)

143. Reorder List(js)

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 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-&

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

Java for LeetCode 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}. 解题思路一: 每次将Ln换到前面,得到L0→Ln→L1→L2→L3→,然后对L1使用相同操作,

LeetCode OJ 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}. Subscribe to see which companies asked this quest

leetcode 143. Reorder List ----- 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}. 按照题意改变链表结构. 1.使用list记录链表. /** * Definition for si

[leetcode]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 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: G

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

Leetcode catelogue

1. Array & List 1.1Sort Array的变更操作,好好运用尾指针:88题的end,75题的blueHead 88. Merge Sorted Array (Array) 75. Sort Colors 21. Merge Two Sorted Lists 23. Merge k Sorted Lists 128. Longest Consecutive Sequence 147. Insertion Sort List 148. Sort List 1.2 Rejust Li