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 思路:反转链表很简 ...
随机推荐
- Identity Service
Identity Service - 解析微软微服务架构eShopOnContainers(二) 接上一篇,众所周知一个网站的用户登录是非常重要,一站式的登录(SSO)也成了大家讨论的热点.微软在 ...
- Java @Validated 遇到的大坑
我在一个Controller内,在两个方法内使用@Validated,这是两个POST方法会进入的方法,这两个方法的实体类的命名(下图红框内容)不能一样,一样的话就会导致第二个在页面显示不出来错误信息 ...
- c#基础-构造函数 this new
构造函数作用:帮助我们初始化对象(给对象的每个属性依次的赋值)构造函数是一个特殊的方法:1).构造函数没有返回值,连void也不能写.2).构造函数的名称必须跟类名一样. 创建对象的时候会执行构造函数 ...
- 在docker上centos7 编译安装php7
docker镜像来自daocloud.io/library/centos 首先下载libmcrypt库并make && make install yum -y install gcc ...
- w3c万维网的介绍和html基本构成
怎么与浏览器交互? 1.鼠标 2.键盘输入 w3c标准: 中文名:万维网联盟!外文名:world wide web cansortium万维网联盟创建于1994年,是web技术领域最具权威个影响的国际 ...
- MySQL-基本概念
一.Mysql逻辑架构 引用自<高性能Mysql> 二.并发控制 读写锁:读锁是共享的,写锁是排他的,会阻塞其他的写锁和读锁. 锁粒度:表锁.行级锁 三.事务 事务(ACID特性):原子性 ...
- 转:error LNK2005: ...already defined in MSVCRTD.lib
编译错误: Error1error LNK2005: _CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.l ...
- MySQL查询示例
use test; create table t1(tid smallint(5) unsigned auto_increment,tname varchar(50),tkecheng varchar ...
- 一个.java文件内只能写一个class吗
先给结论:当然不是!! 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致.一个文件中可以不含public类,如果只有一个非public类,此时可以跟文件名不同. 为 ...
- ECshop安装提示cls_image::gd_version() 和不支持JPEG
ecshop版本:ECShop_V2.7.3_UTF8_release1106php 版本 5.5--------------------------------------------------- ...