题目链接: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. i.MX RT1010之FlexIO模拟I2S外设

    恩智浦的i.MX RT1010是跨界处理器产品,作为i.MX RT跨界MCU系列的一个新的切入点,i.MX RT1010是成本最低的LQFP封装方式与i.MX RT系列产品一贯的高性能和易用性的结合产 ...

  2. leetCode练题——38. Count and Say

    1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...

  3. 《梳理业务的三个难点》---创业学习---训练营第二课---HHR---

    一,<开始学习> 1,融资的第一步:把业务一块一块的梳理清楚. 2,预热思考题: (1)投资人会问能做多大,天花板怎么算?你的答案可以得到大家的认同吗?(四种方法,直接引用,自顶向下,自底 ...

  4. Debian9安装Metasploit

    参考博文:https://www.jianshu.com/p/ea0629b9e367 0x0 添加Kali源 deb http://http.kali.org/kali kali-rolling m ...

  5. 洛谷P2758编辑距离(线性DP)

    题目描述 设A和B是两个字符串.我们要用最少的字符操作次数,将字符串A转换为字符串B.这里所说的字符操作共有三种: 1.删除一个字符: 2.插入一个字符: 3.将一个字符改为另一个字符: !皆为小写字 ...

  6. Update(Stage4):spark_rdd算子:第2节 RDD_action算子_分区_缓存:算子和分区

    一.reduce和reduceByKey: 二.:RDD 的算子总结 RDD 的算子大部分都会生成一些专用的 RDD map, flatMap, filter 等算子会生成 MapPartitions ...

  7. C++98常用特性介绍——mutable关键字

    讲mutable前,先讲一下const函数,讲const函数前,先讲一下函数前后加const的区别 一.C++函数前后加const的区别 1)函数前加const:普通函数或非静态成员函数前均可加con ...

  8. dubbo的dispatcher设置原理

    在上回<Dubbo源代码实现六>中我们已经了解到,对于Dubbo集群中的Provider角色,有IO线程池(默认无界)和业务处理线程池(默认200)两个线程池,所以当业务的并发比较高,或者 ...

  9. SVN安装不成功,提示Invalid driver H:

    本来我的SVN安装在H盘,后来我把包含H盘的硬盘下下来了,这样H盘就不存在了. 这时候我想重新安装SVN,点击安装包,结果提示Invalid driver H,怎么都不能安装成功. 这时候我去注册表里 ...

  10. scp 远程文件拷贝命令

    Linux scp命令用于Linux之间复制文件和目录. scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 1.从本地复制到远程 命令格式: ...