题目链接:https://leetcode.com/problems/reverse-linked-list/

方法一:迭代反转

https://blog.csdn.net/qq_17550379/article/details/80647926讲的很清楚

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pre = NULL;
ListNode* cur = head;
ListNode* lat;
while(cur){
lat = cur -> next;
cur -> next = pre;
pre = cur;
cur = lat;
}
return pre;
}
};

方法二:递归反转

解决递归问题从最简单的case入手。

(1)例如,对于链表1->2->3->4->5,

reverseList(head)返回的是5->4->3->2->1;

reverseList(head->next)返回的是5->4->3->2;(因为head->next所指代的链表是2->3->4->5->NULL)

以此类推。

(2)对于reverseList(3)这个情况,此时head为3,head->next为4,此时链表情况如下:

->->-><-

head->next->next=head这一操作后,链表变为:

然后,head->next=NULL,即

->-><-<- 
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == NULL || head -> next == NULL)
return head;
ListNode *ans = reverseList(head -> next);
head -> next -> next = head;
head -> next = NULL;
return ans;
}
};

此外,提供一个错误写法:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == NULL || head -> next == NULL)
return head;
ListNode *ans = reverseList(head -> next);
ans -> next = new ListNode(head -> val);
return ans;
}
};

这种写法的错误之处在于:

以reverseList(3)为例,虽然ListNode *ans = reverseList(head -> next);返回的是5->4这个链表,不过ans指的是这个链表的第一个结点5,然后ans->next=new ListNode(head->val)后,其实在当前这一层递归中最终return的是5->3这个链表,并不是返回想象中的5->4->3这个链表。

参考:https://blog.csdn.net/qq_17550379/article/details/80647926

LeetCode206. Reverse Linked List(反转链表)的更多相关文章

  1. [LeetCode] 206. Reverse Linked List ☆(反转链表)

    Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3-> ...

  2. [leetcode]206. Reverse Linked List反转链表

    Reverse a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...

  3. 206 Reverse Linked List 反转链表

    反转一个单链表.进阶:链表可以迭代或递归地反转.你能否两个都实现一遍?详见:https://leetcode.com/problems/reverse-linked-list/description/ ...

  4. 91. Reverse Linked List 反转链表

    网址:https://leetcode.com/problems/reverse-linked-list/ 直接参考92:https://www.cnblogs.com/tornado549/p/10 ...

  5. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

  8. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

  9. LeetCode 206. Reverse Linked List倒置链表 C++

    Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...

随机推荐

  1. 今天启动项目的时候报了一个错MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.

    从报错信息看应该是内存问题导致不能持久化到硬盘,在网上找到了一个解决方法: Redis被配置为保存数据库快照,但它目前不能持久化到硬盘.用来修改集合数据的命令不能用.请查看Redis日志的详细错误信息 ...

  2. 批量导出存储在msdb库的SSIS包

    http://blog.51cto.com/ultrasql/1924464 use msdb go IF OBJECT_ID('msdb.dbo.usp_ExportSSISPkgs') IS NO ...

  3. Centos7 将应用添加快捷方式到applications 中以pycham为例[ubuntu]适用

    安装版本pycharm-2019.1.3 安装路径:/opt/pycharm-2019.1.3/ vim /usr/share/applications/pycharm.desktop #!/usr/ ...

  4. PHP弱类型(一)

    如图,id的值必须满足上述表达式,才能返回想要的值 与运算,只要出现false,整个表达式返回false 看见后面的==就想尝试一下弱类型绕过,参考资料:https://www.cnblogs.com ...

  5. 加密设备NAT对IPSec的影响

    加密设备NAT对IPSec VPN的影响:我们先配置好经典的IPSec VPN,然后在R3上做PAT看会对IPSec VPN产生什么影响(不会对有隧道的IPSec VPN技术产生影响). 现在默认配置 ...

  6. mysql 默认信息

    泰基MYSQL默认信息 登录名1-------默认用户 名字:root 密码:123 登录名2-------APP对应的数据库 名字:hotekey 密码:8888

  7. ANSYS热分析简介1

    目录 1. ANSYS热分析简介 1.1 传导 1.2 热载荷分类 1.2.1 载荷施加 1.3 热分析分类 1.3.1 稳态热分析 1.3.2 瞬态热分析 1.3.3 非线性分析综述 2. 热分析单 ...

  8. Android、iOS与Servlet接口上传文件和JSON串的交互

    package etcom.servlet; import java.io.File; import java.io.IOException; import java.sql.Connection; ...

  9. 转:HttpClient使用详解

    一.使用方法 使用HttpClient发送请求.接收响应很简单,一般需要如下几步即可. 1. 创建HttpClient对象. 2. 创建请求方法的实例,并指定请求URL.如果需要发送GET请求,创建H ...

  10. 获取Linux系统运行情况信息

    代码: #include <stdio.h> #include <unistd.h> /* usleep() */ #include <stdlib.h> #inc ...