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. Spring实战第一章学习笔记

    Spring实战第一章学习笔记 Java开发的简化 为了降低Java开发的复杂性,Spring采取了以下四种策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面 ...

  2. centos7源码安装cloud-init

    <template> <name>centos72-source</name> <os> <name>CentOS-7</name&g ...

  3. Daily Scrum02 11.30

    纵然编译大作业压顶,大家还是顶住压力,保证了软工项目的持续进行. Member Today's Task Tomorrow's Task 李孟 Task856 熟悉单元测试方法熟悉单元测试方法 Tas ...

  4. 算法(12)Pascal's Triangle II

    题目:输出帕斯卡三角的第k行 思路:真没思路,发现几个easy的题不容易想!这里的大致思路是从后开始更新第k行!

  5. 详细介绍javascript中的几种for循环的区别

    偶然间见到了forEach循环,感觉很新奇,就研究了一下,顺带着把js中的几种for循环做了一个比较. 首先,简单说一下,js中一共大概有四种for循环:(1).那种简单常见的for循环:(2).fo ...

  6. mysql 数据包太小会引发错误信息

    Error querying database.  Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for quer ...

  7. web颜色转换为delphi

    今天在写写一个日志浏览和报警功能时,要求用多种颜色去显示不同的信息,客户给出的颜色是web的 rgb颜色,就是用6位16进制数去表示的颜色,直接把他赋值给Delphi的TColor变量,发现显示的颜色 ...

  8. 安装和配置hadoop集群步骤

    hadoop集群的安装步骤和配置 hadoop是由java语言编写的,首先我们肯定要在电脑中安装jdk,配置好jdk的环境,接下来就是安装hadoop集群的步骤了,在安装之前需要创建hadoop用户组 ...

  9. Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  10. Android逆向之旅---爆破一款资讯类应用「最右」防抓包策略原理分析

    一.逆向分析 首先感谢王同学提供的样本,因为王同学那天找到我咨询我说有一个应用Fiddler抓包失败,其实对于这类问题,我一般都会这么回答:第一你是否安装Fiddler证书了,他说他安装了.第二你是否 ...