【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}.

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

【题意】

给定一个链表,把最后一个结点插入到第1个结点后,倒数第二个结点插入到第2个结点后,倒数第三个结点插入到第3个结点后,以此类推……

【思路】

由题意知,后面 (n-1)/2 个结点需要分别插入到前面 (n-1)/2 个结点后。

那么先把链表分为两段,前面 n-(n-1)/2 个结点为被插入链表,和后面 (n-1)/2 个结点为插入链表。

在插入之前,需先把插入链表逆序,即第n个结点->第n-1个结点->...

【Java代码】

public class Solution {
    public void reorderList(ListNode head) {
        ListNode node = head;
        int cnt = 0;
        while (node != null) {
            cnt++;
            node = node.next;
        }

        if (cnt < 3) return;//3个以下的结点不需要移动

        int k = (cnt - 1) / 2;//需要移动的后k个结点
        int i = 1;
        node = head;
        while (i++ < cnt - k) {
            node = node.next;
        }
        ListNode begin = node.next;//用begin表示需要移动的后k个结点的开始
        node.next = null;//把不需要移动的部分结尾设为null

        //把需要移动的k个结点逆序
        ListNode pre = begin;
        ListNode cur = begin.next;
        begin.next = null;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;

            begin = cur;
            pre = cur;
            cur = next;
        }

        ListNode node1 = head;
        ListNode node2 = begin;
        while (node1 != null && node2 != null) {
            pre = node1;
            cur = node2;

            node1 = node1.next;//这两行一定要放在下面两行之前,因为pre和node1指向同一个结点,下面操作会改变node1的next;cur和node2同理
            node2 = node2.next;

            cur.next = pre.next;//这两行代码是把cur插入到pre后
            pre.next = cur;
        }
    }
}

【感受】

代码写起来超恶心,写着写着就混乱了,一会儿就不知道next指的是哪个结点了。

时间: 2024-10-10 09:32:30

【LeetCode】Reorder List的相关文章

【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}. /** * Definition for singly-linked list. * clas

【Leetcode】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.暴力解法 这种解法所需的时间的时间复杂度比较高,为O(n2) 代码如下

【leetcode】Reorder List (middle)

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 (python)

问题的思路是这样: 循环取头部合并,事实上也能够换个角度来看,就是将后面的链表结点,一次隔空插入到第一部分的链表中. class Solution: # @param head, a ListNode # @return nothing def reorderList(self, head): if None == head or None == head.next: return head #找到中间点,截断 pfast = head pslow = head while pfast.next

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. 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 linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n