Reorder List 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/reorder-list/description/


Description

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

Solution

typedef struct ListNode ListNode;

// reverse linked list with head node
void reverseList(struct ListNode* head) {
if (head == NULL || head -> next == NULL || head -> next -> next == NULL)
return; ListNode *preNode = head -> next;
ListNode *tail = preNode;
ListNode *curNode = preNode -> next;
ListNode *nextNode;
while (curNode != NULL) {
nextNode = curNode -> next;
curNode -> next = preNode;
preNode = curNode;
curNode = nextNode;
} head -> next = preNode;
tail -> next = NULL;
} void reorderList(struct ListNode* head) {
if (head == NULL || head -> next == NULL || head -> next -> next == NULL)
return;
ListNode *mid = head, *tail = head -> next;
while (tail && tail -> next) {
mid = mid -> next;
tail = tail -> next -> next;
}
reverseList(mid);
ListNode *qNode = mid -> next, *qNextNode;
mid -> next = NULL;
ListNode *preNode = head;
ListNode *curNode = preNode -> next;
while (curNode != NULL) {
preNode -> next = qNode;
qNextNode = qNode -> next;
qNode -> next = curNode;
preNode = curNode;
curNode = curNode -> next;
qNode = qNextNode;
} preNode -> next = qNode;
}

解题描述

这道题刚拿上手还是有点不知所措。想得清楚怎么画图,就是不知道怎么实现。后面才慢慢想出,在原来的链表中间的地方截断成2个链表,将后半截用带头节点链表反转的方式进行反转,之后再合并这两个量表。主要还是考验了链表的操作,包括:

  1. 快速找到链表中间节点
  2. 反转链表
  3. 链表合并

[Leetcode Week6]Reorder List的更多相关文章

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

  2. [LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序

    You have an array of `logs`.  Each log is a space delimited string of words. For each log, the first ...

  3. 【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 thi ...

  4. 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 th ...

  5. 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 thi ...

  6. [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

    For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...

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

  8. LeetCode Solutions : Reorder List

    →-→Ln-1→Ln, reorder it to: L→Ln-2→- You must do this in-place without altering the nodes' values. Fo ...

  9. Leetcode 143. Reorder List(Medium)

    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 thi ...

随机推荐

  1. Charles的Https抓包及弱网配置

    一.Charles的主要功能 (1)截取Http 和 Https 网络封包. (2)支持重发网络请求,修改请求参数,方便后端调试. (3)支持模拟弱网环境. 二.配置简单抓包 1.设置系统代理:勾选P ...

  2. BZOJ1222[HNOI 2001]产品加工

    题面描述 某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所需的时间会不同,若同时由两台机器共同进行加工 ...

  3. 数据结构14——AC自动机

    一.相关介绍 知识要求 字典树Trie KMP算法 AC自动机 多模式串的字符匹配算法(KMP是单模式串的字符匹配算法) 单模式串问题&多模式串问题 单模就是给你一个模式串,问你这个模式串是否 ...

  4. 【Linux】使用 PXE+Kickstart 无人值守批量安装系统

    一.PXE背景知识 通过 PXE+DHCP+TFTP+VSftpd+Kickstart 服务程序搭建出无人值守安装系统,从而批量部署客户机系统. PXE(Preboot eXecute Environ ...

  5. 物联网PPT智能家居王思齐和陈由钧第10组

    ppt做完了但是不知道怎么用博客园发ppt!只能发几个图片了

  6. C#怎么调用方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Exep ...

  7. BZOJ5110 CodePlus2017Yazid 的新生舞会(线段树)

    考虑统计每个数字的贡献.设f[i]为前缀i中该数的出现次数,则要统计f[r]-f[l]>(r-l)/2的数对个数,也即2f[r]-r>2f[l]-l. 注意到所有数的f的总变化次数是线性的 ...

  8. 【题解】SDOI2014旅行

    洛谷P3313 大概是一道树链剖分的裸题.可以看出如果不是查询相同宗教的这一点,就和普通的树链剖分毫无两样了.所以针对每一个宗教都单独开一棵线段树,变成单点修改+区间查询.只不过宗教数目很多,空间消耗 ...

  9. BZOJ1558 [JSOI2009]等差数列 【线段树】

    题目链接 BZOJ1558 题解 等差数列,当然是差分一下 差分值相同的连续位置形成等差数列,我们所选的两个等差数列之间可以有一个位置舍弃 例如: \(1 \; 2 \; 3 \; 6 \; 8 \; ...

  10. Saruman’s Level Up~(多校赛算组合数)

    Description Saruman’s army of orcs and other dark minions continuously mine and harvest lumber out o ...