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. jmeter如何连接数据库

    大家都知道jmeter是java编写的,java/jmeter如果想连接数据库就要通过java database connector(JDBC)去连接,首先需先下载一个驱动 (mysql-connec ...

  2. 分布式文件系统---GlusterF

      1.1 分布式文件系统 1.1.1 什么是分布式文件系统 相对于本机端的文件系统而言,分布式文件系统(英语:Distributed file system, DFS),或是网络文件系统(英语:Ne ...

  3. 安装arch系统时,把ubuntu的efi分区格式化

    导致无法从grub进入ubuntu,之后我进入win10,把ubuntu的分区都删了. 再重启,只能进入黑色的grub界面,显示 grub>> 甚至无法进入win10.只能通过在开机时按F ...

  4. UnrealEngine4入门(二) 实现一个可用按键控制的球体

    源码摘自官网guide,加上部分自己的理解和注释 接上篇博客 本文实现一个可用WASD控制滚动的球体 先创建一个可见的球体: 在CollidingPawn.cpp的构造函数ACollidingPawn ...

  5. grpc deadlines

    最近在将应用的rpc更换为grpc,使用过程中,发现报“rpc error:code=DeadlineExceeded desc = context deadline exceeded”,这是啥?原来 ...

  6. Jekyll 使用入门

    Jekyll 是一个网站生成工具,可以用来将带有一定格式的文本(如:MarkDown)转换成静态的HTML页面, 并提供了Liquid模板引擎进行页面渲染,然后可以将生成的静态网站发布到如 Githu ...

  7. dechex()

    dechex() 函数把十进制转换为十六进制生成验证码的时候用到了

  8. oracle视图就是封装了一条写好的sql语句 可通过视图修改表结构 ; oracle需要手动创建序列

    create sequence student_sid; --创建序列 oracle只能通过手动方式创建序列

  9. JavaScript归并方法reduce()和reduceRight()

    ECMAScript 5还新增了两个归并数组的方法:reduce()和reduceRight().这两个方法都会迭代数组的所有项,然后构建一个最终返回的值.其中,reduce()方法从数组的第一项开始 ...

  10. BZOJ4513 SDOI2016储能表(数位dp)

    如果n.m.k都是2的幂次方,答案非常好统计.于是容易想到数位dp,考虑每一位是否卡限制即可,即设f[i][0/1][0/1][0/1]为第i位是/否卡n.m.k的限制时,之前的位的总贡献:g[i][ ...