题目链接: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. web渗透(转)

    某天比较无聊,听一个朋友推荐httpscan这款工具,于是就下载下来试试. 首先对某学校网段开始进行测试. 1   python httpscan.py **.**.**.0/24 测试时发现有个比较 ...

  2. properties配置文件在idea中默认utf-8可能会乱码

    改一个设置就好了

  3. 开源沙箱CuckooSandbox 介绍与部署

    1. 介绍 1.1应用  在工作中很多时候需要自己对一些可以程序,可执行文件进行检测,当然我们可以通过VT,微步,等一些开源的平台进行检测.现在我们通过自己搭建的开源的沙箱进行检测.所谓沙箱,是分离运 ...

  4. ZOJ4104 Sequence in the Pocket(2019浙江省赛)

    思维~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int N; int main ( ...

  5. Spring Boot JDBC 使用教程

    总是要用到数据库的嘛,曾经我一度以为,写代码,编程就是搞数据库增删改查,甚至你设计一个系统,大部分时候在为如何设计关系型数据库努力,究其原因,是因为关系型数据库是逻辑的主要呈现. 这个系列,主要是对 ...

  6. 夯实Java基础(二十五)——JDBC使用详解

    1.JDBC介绍 JDBC的全称是Java Data Base Connectivity(Java数据库连接).是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问(例如MyS ...

  7. mcast_join函数

    #include <errno.h> #include <string.h> #include <net/if.h> #include <netinet/in ...

  8. office 格式定义

    在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串转换成 科学计数法.现在网上找到解决方案之一: (在数字串 ...

  9. Python - unittest打印成功信息

    参考 https://stackoverflow.com/questions/36834677/print-success-messages-for-asserts-in-python 总结 clas ...

  10. Python 的直接赋值、Deepcopy、Copy的区别

    直接赋值: 其实就是对象的引用 浅拷贝(copy): 只拷贝符对象,不会拷贝对象内部的子对象 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象 有图有 ...