题目:

Reverse a linked list.

Example

For linked list 1->2->3, the reversed linked list is 3->2->1

题解:

Solution 1 ()

class Solution {
public:
ListNode *reverse(ListNode *head) {
if (!head) {
return head;
}
ListNode *prev = nullptr;
while (head) {
ListNode *tmp = head->next;
head->next = prev;
prev = head;
head = tmp;
} return prev;
}
};

  The basic idea of this recursive solution is to reverse all the following nodes after head. Then we need to set head to be the final node in the reversed list. We simply set its next node in the original list (head -> next) to point to it and sets its next to be NULL.

Solution 2 ()

class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head || !(head -> next)) return head;
ListNode* node = reverseList(head -> next);
head -> next -> next = head;
head -> next = NULL;
return node;
}
};

疑问?为啥下面这段程序是错的

class Solution {
public:
ListNode *reverse(ListNode *head) {
if (!head) {
return head;
}
ListNode *prev = nullptr;
while (head) {
ListNode *tmp = head;
head->next = prev;
prev = head;
head = tmp->next; } return prev;
}
};

  若一定要tmp保存head,那么程序应该如下

class Solution {
public:
ListNode *reverse(ListNode *head) {
if (!head) {
return head;
}
ListNode *prev = nullptr;
while (head) {
ListNode *tmp = new ListNode(-);
*tmp = *head;
head->next = prev;
prev = head;
head = tmp->next;
delete tmp;
} return prev;
}
};

解惑:

int main()
{
ListNode* tmp = new ListNode();
ListNode* p = new ListNode();
ListNode* q = new ListNode();
ListNode* r = new ListNode();
ListNode** t = &tmp;
tmp->next = p;
p->next = q;
q->next = r; t = &((*t)->next);
(*t) = (*t)->next; cout << (*t)->val << endl;
cout << tmp->next->val << endl;
cout << (*p).val << endl; return ;
}

输出为:2 2 1

【Lintcode】 035.Reverse Linked List的更多相关文章

  1. 【Lintcode】036.Reverse Linked List II

    题目: Reverse a linked list from position m to n. Given m, n satisfy the following condition: 1 ≤ m ≤ ...

  2. 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)

    题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...

  3. 【LeetCode】206. Reverse Linked List (2 solutions)

    Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ...

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

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

  5. 【LeetCode】206. Reverse Linked List

    题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...

  6. 【leetcode】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-> ...

  7. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

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

  8. 【easy】206. Reverse Linked List 链表反转

    链表反转,一发成功~ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; ...

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

随机推荐

  1. Jquery 常用方法总结

    1.Attribute(属性): $(”p”).addClass(css中定义的样式类型); 给某个元素添加样式 $(”img”).attr({src:”test.jpg”,alt:”test Ima ...

  2. JVM完全指南

    JVM完全指南     一:虚拟机内存图解   JAVA程序运行与虚拟机之上,运行时需要内存空间.虚拟机执行JAVA程序的过程中会把它管理的内存划分为不同的数据区域方便管理.     虚拟机管理内存数 ...

  3. java eclipse使用不同jdk版本

    因为开发需要,两个工程分别需要使用jdk1.6(elipse indigo)和jdk1.8(eclipse neon).因为两个eclipse对于jdk版本的要求不同,若只在环境变量中配置jdk版本, ...

  4. Java学习之路 第四篇 oop和class (面向对象和类)

    本人水平有限,创作本文是为了记录学习和帮助初学者学习,欢迎指正和补充 一.面向对象编程的设计概述 很多同学都在学校学了电脑的编程,现在的书籍大部分都是oop面向对象编程,一个很抽象的的名字,比较难以理 ...

  5. 【ASP.NET】巧用Cookie实战

    上篇介绍了究竟什么是Cookie.究竟是干什么用的,这篇博客具体具体的说一下.Cookie究竟怎样用. 首先建立如图所看到的的界面.通过该界面可登录到某个站点.详细要求例如以下: ·在首次登录后,将登 ...

  6. UVALive 6530 Football (水

    题目链接:点击打开链 #include <cstdio> #include <vector> #include <algorithm> using namespac ...

  7. String知识点

  8. C#操作XML方法:新增、修改和删除节点与属性

    一 前言 先来了解下操作XML所涉及到的几个类及之间的关系  如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家 * 1 XMLElement 主要是针对节点的一些属性进行操 ...

  9. Python文件编码不可以使用UTF16

    1. The complete Python source file should use a single encoding. Embedding of differently encoded da ...

  10. (Android)react-native-splash-screen实践-解决react-native打包好后启动白屏的问题

    1.安装 npm i react-native-splash-screen --save or yarn add react-native-splash-screen --save 2.自动配置 re ...