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

▶ 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. Wannafly挑战赛15-C-出队

    链接:https://www.nowcoder.com/acm/contest/112/C来源:牛客网 约瑟夫问题(https://baike.baidu.com/item/约瑟夫问题),n个人,1 ...

  2. HDU-4511-ac自动机+dp

    小明系列故事——女友的考验 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  3. Leetcode 51

    //看了一次解析后,一次AC,用一个pos记录行列.class Solution { public: vector<vector<string>> solveNQueens(i ...

  4. python学习笔记(六)---sublime text3 创建python项目

    1.创建项目 依次鼠标左键点击Project>Add Folder to Project...,选择test文件夹: 2.保存项目 依次鼠标左键点击Project>Save Project ...

  5. Uedit个人专注

    Uedit个人专注   Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\Shell\Uedit] [HKEY_CLASSES_ROO ...

  6. 解决Myeclipse闪退问题

    才安装好Myeclipse就出了问题,打开之后没过多久就自动退出了,看了好多解决方法都无效,后来才找到正确路径,转载过来方便跟我遇到同样问题的小伙伴,尽快解决 转载自:http://blog.csdn ...

  7. MMAP文件内存映射

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  8. 201621123010 《Java程序设计》第1周学习总结

    1. 本周学习总结 本周主要学习了五个知识点 Java是面向对象的语言 JDK,JRE,JVM的联系 javac及java指令 Java跨平台运行的原理 新概念:类,类是面向对象中的概念 2. 书面作 ...

  9. C语言extern关键字使用

    在chinaunix上看见一篇转载的文章,觉得特别好,关于extern使用的解释: 参考链接:http://doc.chinaunix.net/CPP/201206/2248432.shtml 在C语 ...

  10. $.each()的用法

    var obj = { one:1, two:2, three:3}; $.each(obj, function(key, val) { console.log(key);//one two thre ...