▶ 关于单链表翻转的两个问题。

▶ 206. 翻转整个单链表。

● 自己的代码,9 ms,使用了递归。

 class Solution
{
public:
ListNode* reverseList(ListNode* head)
{
if (head == nullptr)
return nullptr;
ListNode *p;
for (p = head; p->next != nullptr; p = p->next);// 找到末元(翻转后的首元)
reverseNode(head);
return p;
}
inline void reverseNode(ListNode* p)// 翻转以 p 指向的结点为首元的单链表,并返回一个指向末元的指针(方便下一次挂上新的首元)
{
if (p == nullptr || p->next == nullptr)
return;
reverseNode(p->next); // 翻转除首元意外的部分
p->next->next = p; // 把首元挂到最后
p->next->next->next = nullptr; // 去掉成环的部分
return ;
}
};

● 大佬的代码,9 ms,逐格移动。

 class Solution
{
public:
ListNode* reverseList(ListNode* head)
{
if (head == NULL || head->next == NULL)
return head;
ListNode *prev, *cur, *temp;
for(prev = NULL, cur = head; cur != NULL;)
{
temp = cur->next;
cur->next = prev;
prev = cur;
cur = temp;
}
return prev;
}
};

▶ 92. 要求翻转单链表中第 m 到第 n(两端包含,且 m 可以等于 n)之间的所有元。

● 自己的代码,4 ms,使用了第 206 题的结果。

 class Solution
{
public:
ListNode* reverseBetween(ListNode* head, int m, int n)
{
if (head == nullptr)
return head;
ListNode newHead(-), *lp, *rp, *lpprev, *temp;
newHead.next = head;
int i, j;
for (i = , lpprev = &newHead; i < m && lpprev != nullptr; i++, lpprev = lpprev->next);// 找到第 m-1 元和第 m 元
if (lpprev == nullptr || (lp = lpprev->next) == nullptr)
return head;
for (j = i, rp = lp; j < n && rp != nullptr; j++, rp = rp->next);// 找到第 n 元
if (rp == nullptr)
return head;
temp = rp->next; // 尾部不翻转部分的首元
rp->next = nullptr; // 断开翻转部分的尾部链接
lpprev->next = reverseList(lp);// 调用翻转函数
lp->next = temp; // 重新接上尾部
return newHead.next;
}
ListNode* reverseList(ListNode* head)
{
if (head == nullptr)
return nullptr;
ListNode *p;
for (p = head; p->next != nullptr; p = p->next);// 找到末元(翻转后的首元)
reverseNode(head);
return p;
}
inline void reverseNode(ListNode* p)// 翻转以 p 指向的结点为首元的单链表
{
if (p == nullptr || p->next == nullptr)
return;
reverseNode(p->next); // 翻转除首元意外的部分
p->next->next = p; // 把首元挂到最后
p->next->next->next = nullptr; // 去掉成环的部分
return;
}
};

● 大佬的代码,4 ms,也是使用逐格移动。

 class Solution
{
public:
ListNode* reverseBetween(ListNode* head, int m, int n)
{
ListNode *first = new ListNode(), *t_head, *first_reverse, *node;
first->next = head;
t_head = first;
for (int i = ; i<m - ; t_head = t_head->next, i++)
first_reverse = t_head->next;
node = t_head->next;
for (int i = m; i <= n; i++)
{
ListNode * temp = node->next;
node->next = t_head->next;
t_head->next = node;
node = temp;
}
first_reverse->next = node;
return first->next;
}
};

▶ 一个副产品,交换单链表中的两个元素。我已开始把第 92 题理解错了,以为只是交换单链表中第 m 和第 n 个元素,所以写成了下面的东西。

 class Solution
{
public:
ListNode* reverseBetween(ListNode* head, int m, int n)
{
if (head == nullptr)
return head;
ListNode newHead(-), *lp, *rp, *lpprev, *rpprev, *temp;
newHead.next = head;
int i, j;
for (i = , lpprev = &newHead; i < m && lpprev != nullptr; i++, lpprev = lpprev->next);
if (lpprev == nullptr || (lp = lpprev->next) == nullptr)
return head;
for (j = i, rpprev = lpprev; j < n && rpprev != nullptr; j++, rpprev = rpprev->next);
if (rpprev == nullptr || (rp = rpprev->next) == nullptr)
return head;
lpprev->next = rp;
rpprev->next = lp;
swap(lp->next, rp->next);
return newHead.next;
}
};

206. Reverse Linked List + 92. Reverse Linked List II的更多相关文章

  1. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  2. 92. Reverse Linked List II

    题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...

  3. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  4. 链表 206 Reverse Linked List, 92,86, 328, 2, 445

    表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...

  5. 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List

    The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three poi ...

  6. [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  7. [LeetCode] 92. Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  8. [LeetCode] 92. Reverse Linked List II 反向链表II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  9. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

随机推荐

  1. SCWS中文分词PHP扩展详细安装说明

    因最近写的一段代码,需要用到中文分词,在网上找了一下,发现了scws这个不错的插件,故根据文档安装使用,下面记录下安装的全过程 系统:centos 安装scws wget http://www.xun ...

  2. C语言----------链表的简单实现与操作

    链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的. 链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成. 每个结点包括两个部 ...

  3. Java 动态代理与反射机制

    java动态代理必须的两个类与两个接口: 首先需要有一个接口(委托者需要实现该接口的方法)示例如下: <pre name="code" class="html&qu ...

  4. EM算法及其应用: K-means 与 高斯混合模型

    EM算法及其应用(一) EM算法及其应用(二): K-means 与 高斯混合模型 上一篇阐述了EM算法的主要原理,这一篇来看其两大应用 -- K-means 与 高斯混合模型,主要由EM算法的观点出 ...

  5. Alpha阶段第1周 Scrum立会报告+燃尽图 02

    作业要求与https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246相同 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 周 ...

  6. mongdb分布式

    搭建高可用mongodb集群(一)——配置mongodb Posted on 17 十一月, 2013 by lanceyan | 21 Replies 在大数据的时代,传统的关系型数据库要能更高的服 ...

  7. Django 之 用redis存储session

    方案I: 1. 安装 django-redis liuqian@ubuntu:~$ pip install django-redis dango-redis 官方文档:http://niwinz.gi ...

  8. word 使用中 上标符号的实现

    1.   首先在word 中打下一段话  如:   啦啦啦啦啦啦啦啦  然后加入你需要的上标   如   [2] 2.    选中你需要的上标,然后右击 3.   点击字体选项 出现下图: 4.  在 ...

  9. 编写 Target 检测 MSBuild / dotnet build 此次编译是否是差量编译

    MSBuild 或 Roslyn 编译项目时均支持差量编译,毕竟为了性能.我在 每次都要重新编译?太慢!让跨平台的 MSBuild/dotnet build 的 Target 支持差量编译 一文中介绍 ...

  10. Error in WCF client consuming Axis 2 web service with WS-Security UsernameToken PasswordDigest authentication scheme

    13down votefavorite 6 I have a WCF client connecting to a Java based Axis2 web service (outside my c ...