LeetCode(206) Reverse Linked List
题目
Reverse a singly linked list.
click to show more hints.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
分析
反转链表。
一个简单的解法,既然反转该链表,我们把所有节点作为一个输入序列,按照头插法重新构造一个链表即可,它既是所给链表的反转结果。
题目所给提示还有另外两种方法解决,迭代和递归。
没有想到所说的迭代是个什么意思,下面将给出头插法和递归实现的代码!
AC代码
class Solution {
public:
//方法一:头插法
ListNode* reverseList(ListNode* head) {
if (head == NULL)
return head;
//反转,即将所有节点按照头插法重新插入一遍即可
ListNode *p = head->next;
head->next = NULL;
while (p)
{
//保存p的后续节点
ListNode *r = p->next;
p->next = head;
head = p;
p = r;
}
return head;
}
//方法二:递归实现
ListNode* reverseList2(ListNode* head) {
if (head == NULL)
return head;
ListNode *p = head;
//反转其余节点组成的链表,将头结点链接到尾部
if (head->next)
{
head = reverseList(head->next);
ListNode *r = head;
while (r->next)
r = r->next;
r->next = p;
}
p->next = NULL;
return head;
}
};
LeetCode(206) Reverse Linked List的更多相关文章
- 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- ...
- LeetCode(7)Reverse Integer
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...
- leetcode之旅(9)-Reverse Linked List
题目描述: Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed ei ...
- LeetCode(234) Palindrome Linked List
题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...
- LeetCode(151) Reverse Words in a String
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- LeetCode(25)Reverse Nodes in k-Group
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- LeetCode(47)-Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- LeetCode(190) Reverse Bits
题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- Leetcode(206)-反转链表
反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 思路:反转链表很简 ...
随机推荐
- NSSM把.Net Core部署至 Windows 服务
NSSM把.Net Core部署至 Windows 服务 https://www.cnblogs.com/emrys5/p/nssm-netcore.html 为什么部署至Windows Servic ...
- openstack安装newton版本keyston部署(一)
一.部署环境: 两台centos7, 内存2G 控制计算节点: Hostname1: ip:172.22.0.218 计算节点及存储节点 Hostnam ...
- Spring-boot原理(附带实现一个spring-boot-starter实例和代码下载)
(我就是个封面) Spring-boot自出现后,到现在火的很,大家貌似都在用,连招聘里面也要求会这个.但是说实话,spring-boot无外乎想实现一种可插拔的编程方式,说是简化配置,其实并没有 ...
- 【0 基础学Dojo】第【1】篇 HelloWord
打开dojo 官网首页 http://dojotoolkit.org/,我们看到 点击get dojo 你将得到下载Dojo 的不同方式 2,点击下面方式下载, 解压后 新建myTest.html, ...
- C++运算符重载讲解与经典实例
最近在学C++,找到一篇详细讲解运算符重载的文章,贴在这里分享和收藏. C++中预定义的运算符的操作对象只能是基本数据类型,实际上,对于很多用户自定义类型,也需要有类似的运算操作.例如: class ...
- Typedef 用法
typedef声明有助于创建平台无关类型,甚至能隐藏复杂和难以理解的语法. 不管怎样,使用 typedef 能为代码带来意想不到的好处,通过本文你可以学习用typedef避免缺欠,从而使代码更健壮. ...
- U3D加载服务器上的assetbundle
在Unity3D中,如果加载服务器上的AssetBundle,总是会提示找不到crossdomain.xml文件,即使添加了该文件,也会报同样的错误.属于跨域访问报错的问题. 官方的解决方案如下: h ...
- django之基于cookie和装饰器实现用户认证
示例1 # Create your views here. user = "a" pwd = "a" def login(request): if reques ...
- webservice、soap、wsdl
搜集了一些关于webservice.soap.wsdl的基本知识,解决工作中遇到的疑问 一 .什么是webservice(用你的话描述webservice)?在什么时候用webservice(webs ...
- .net 实现的上传下载,如果是图片就显示上传的图片
HTML: <div> <input id="fileUpload" type="file" runat="server" ...