题目链接: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. 使用MQTT.fx客户端接入阿里云

    一.前期准备. 1.关于MQTT连接的属性:https://www.cnblogs.com/mhtc/p/11112153.html 2.关于阿里云配置工具的使用:https://www.cnblog ...

  2. Flex:实例

    目的: 代码: <!--pages/index/index.wxml--> <view class="container"> <view class= ...

  3. Vacuum Pump Manufacturer - Vacuum Pump: Prevents Reactive Compound Decomposition Products

    Vacuum packaging has been popular in the industry for a long time. Many large companies have joined ...

  4. python opencv:代码执行时间计算

    t1 = cv2.getTickCount() # ...... t2 = cv2.getTickCount() # 计算花费的时间:毫秒 time = (t2-t1) / cv2.getTickFr ...

  5. POJ-3821-Dining (拆点网络流)

    这题为什么不能用 左边放食物,中间放牛,后面放水? 原因很简单,假设一头牛喜欢两个食物AB和两种水AB. 此时可以从一个食物A,走到牛A,再走到水A. 但是还可以有另一条路,从另一个食物B,走到该牛A ...

  6. 【代码审计】VAuditDemo 重装漏洞

    一.源码安装漏洞介绍 一般在PHP源码程序都有一个初始安装的功能,如果相关代码没有对参数进行严格过滤,可能会导致攻击者访问安装页面(install.php)或构造数据包,对网站进行重新安装,从而危害网 ...

  7. slf4j-api整合maven 工程日志配置文件

    springmvc项目 pom.xml: <dependency> <groupId>org.slf4j</groupId> <artifactId>s ...

  8. 29 对象&函数

    switch: 穿越: 没有判断结果的情况下执行下一个case的语句块,叫穿越 或者穿越: switch(s%10){ case 1: case 2: case 3: s++; break; defa ...

  9. Hibernate学习(六)

    Hibernate的三种查询方式 1.Criteria 查询 ,Query  By Criteria ( QBC )JPA 规范中定义的一种查询方法,但是不推荐使用 2.HQL : Hibernate ...

  10. 阿里云虚拟主机申请免费SSL证书并成功开通Https访问

    参考文档网址  https://baijiahao.baidu.com/s?id=1628343140232374972&wfr=spider&for=pc