[LeetCode 206] Reverse Linked List 翻转单链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
return reverseList_iteratively(head);
//return reverseList_recursively(head);//递归方法leetcode超时
}
//迭代
ListNode* reverseList_iteratively(ListNode* head)
{
ListNode* p = nullptr;
ListNode* w = nullptr;
while(head)
{
p = head->next;
head->next = w;
w = head;
head = p;
}
return w;
}
//递归
ListNode* reverseList_recursively(ListNode* head)
{
if(head==nullptr||head->next==nullptr)
{
return head;
}
ListNode* h = reverseList_recursively(head->next);
head->next->next=head;//加入反转后的单链表尾部
head = nullptr;
return h;
}
};
[LeetCode 206] Reverse Linked List 翻转单链表的更多相关文章
- LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)
翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
- [LeetCode] 92. Reverse Linked List II 反向链表II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- [LeetCode] 206. Reverse Linked List ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- LeetCode 206 Reverse Linked List 解题报告
题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...
- [LeetCode] 92. Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
随机推荐
- js获取div基础元素
1.js获取div元素 clientHeight 获取对象的高度,不计算任何边距.边框.滚动条,但包括该对象的补白. clientLeft 获取 offsetLeft 属性和客户区域的实际左边之间的距 ...
- wkhtmltopdf 自定义字体未生效或中文乱码
使用wkhtmltopdf控件将网页保存成pdf的过程中出现网页中有些字体,在PDF中未生效.通过网上查询结果有一种处理方式: 在网页头部的style标签中,手工指定宋体字体的本地存放位置,wkhtm ...
- vue的prop父子组件传值
props down, events up 父组件通过 props 向下传递数据给子组件:子组件通过 events 给父组件发送消息. 静态 props 要让子组件使用父组件的数据,需要通过子组件的 ...
- Java8-Stream-No.05
import java.util.Arrays; import java.util.List; import java.util.function.Supplier; import java.util ...
- Java中的集合Queue、LinkedList、PriorityQueue(四)
Queue接口 Queue用于模拟了队列这种数据结构,队列通常是指“先进先出”(FIFO)的容器.队列的头部保存在队列中时间最长的元素,队列的尾部保存在队列中时间最短的元素.新元素插入(offer)到 ...
- python中global的用法——再读python简明教程
今天看了知乎@萧井陌的编程入门指南,想重温一下 <python简明教程>,对global的用法一直不太熟练,在此熟练一下,并实践一下python中list.tuple.set作为参数的区别 ...
- Java学习笔记(持续更新ing)
1.在读入字符串时: str = sc.nextLine(); //读入一行 str = sc.next(); ...
- SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xce in position 0: invalid continuatio
点击 文档>>设置文件编码>>Unicode>>Unicode(UTF-8)
- php原生导出简单word表格(TP为例) (原)
后台: # 菲律宾名单word导出 public function export_word(){ $tids = $_GET['tids']; $userinfo=M("philippi ...
- C#操作 Access 2013(.accdb)的方法
使用的Microsoft.Jet.OLEDB.4.0,的方法并不能连接最新的Access 存储文件,而且Microsoft.Jet.OLEDB.4.0不能使用x64的方式生成,而且使用这个数据库引擎效 ...