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 思路:反转链表很简 ...
随机推荐
- shell 经典
使用新写法 这里的新写法不是指有多厉害,而是指我们可能更希望使用较新引入的一些语法,更多是偏向代码风格的,比如 尽量使用func(){}来定义函数,而不是func{} 尽量使用[[]]来代替[] 尽量 ...
- webpack.config.js====插件html-webpack-plugin
1. 安装 cnpm install html-webpack-plugin --save-dev 2. webpack.config.js中使用 const htmlWebpackPlugin = ...
- 什么是JavaScript
来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=30&cid= JavaScript是一种松散类型的客户端脚本语言,在用户浏览器中执 ...
- ABAP接口用法
1.定义接口INTERFACE intf [PUBLIC]. [components] ENDINTERFACE. 2.注意点: 2.1.接口中所定义的所有东西默认都是公共的,所以不用也不能写PU ...
- Ubuntu 配置IP地址方法
接到一客户的服务器,开机已启动发现是Ubuntu系统,当时有点郁闷了,心想没有配置过ubuntu系统,这客户还在旁边了,心里有点紧张了,于是开始上网寻找各种方法配置,最终将IP配置好,给客户上架调试通 ...
- 编写SQL语句操作数据库(慕课SQLite笔记)
安卓常用数据存储方式之一SQLite学习及操作笔记 0.视频地址:http://www.imooc.com/video/3382 1.每个程序都有自己的数据库 默认情况下是各自互不干扰 1)创建一个数 ...
- javaSe-反射2
//创建实体类 package com.java.chap07.sec03; public class Student { private String name; private Integer a ...
- HDU 4734 F(x) (数位DP,基础)
题意: 一个非负整数的十进制位是这样的 (AnAn-1An-2 ... A2A1),定义F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. ...
- PostgreSQL扫盲教程
在这个链接下载PostgreSQL. 安装时,请记住您给user postgres设置的初始密码,以及默认端口号5432,后面需要使用. 再安装图形化管理UI pgadmin,可以从这个链接获得. 安 ...
- Jquery二维码在线生成(不能生成图片文件)
附件地址:http://files.cnblogs.com/files/harxingxing/jQuery%E4%BA%8C%E7%BB%B4%E7%A0%81%E5%9C%A8%E7%BA%BF% ...