[Algorithm] 206. Reverse Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULLFollow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Approach #1 (Iterative) [Accepted]
Assume that we have linked list 1 → 2 → 3 → Ø
, we would like to change it to Ø ← 1 ← 2 ← 3
.
While you are traversing the list, change the current node's next pointer to point to its previous element. Since a node does not have reference to its previous node, you must store its previous element beforehand. You also need another pointer to store the next node before changing the reference. Do not forget to return the new head reference at the end!
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
Complexity analysis
Time complexity : O(n)O(n). Assume that nn is the list's length, the time complexity is O(n)O(n).
Space complexity : O(1)O(1).
Approach #2 (Recursive) [Accepted]
The recursive version is slightly trickier and the key is to work backwards. Assume that the rest of the list had already been reversed, now how do I reverse the front part? Let's assume the list is: n1 → … → nk-1 → nk → nk+1 → … → nm → Ø
Assume from node nk+1 to nm had been reversed and you are at node nk.
n1 → … → nk-1 → nk → nk+1 ← … ← nm
We want nk+1’s next node to point to nk.
So,
nk.next.next = nk;
Be very careful that n1's next must point to Ø. If you forget about this, your linked list has a cycle in it. This bug could be caught if you test your code with a linked list of size 2.
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
Complexity analysis
Time complexity : O(n)O(n). Assume that nn is the list's length, the time complexity is O(n)O(n).
Space complexity : O(n)O(n). The extra space comes from implicit stack space due to recursion. The recursion could go up to nn levels deep.
[Algorithm] 206. Reverse Linked List的更多相关文章
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- 链表 206 Reverse Linked List, 92,86, 328, 2, 445
表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 206. Reverse Linked List - LeetCode
Question 206. Reverse Linked List Solution 题目大意:对一个链表进行反转 思路: Java实现: public ListNode reverseList(Li ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- 【LeetCode】206. Reverse Linked List (2 solutions)
Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ...
- [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 ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- [刷题] 206 Reverse Linked List
要求 反转一个链表 不得改变节点的值 示例 head->1->2->3->4->5->NULL NULL<-1<-2<-3<-4<-5 ...
随机推荐
- Scala 匹配模式
模式匹配 // Scala是没有Java中的switch case语法的,相对应的,Scala提供了更加强大的match case语法,即模式匹配,类替代switch case,match case也 ...
- 更新Linux内核
说明:为了安装Docker,当前虚拟机不满足要求,版本如下: [root@localhost116 ~]# uname -r -.el6.x86_64 [root@localhost116 ~]# c ...
- MQTT --- Retained Message
保留消息定义 如果PUBLISH消息的RETAIN标记位被设置为1,则称该消息为“保留消息”: Broker会存储每个Topic的最后一条保留消息及其Qos,当订阅该Topic的客户端上线后,Brok ...
- [Codeforces] Alex and a Rhombus
A. Alex and a Rhombus time limit per test 1 second memory limit per test 256 megabytes input standar ...
- C#读写调整UVC摄像头画面-饱和度
有时,我们需要在C#代码中对摄像头的饱和度进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄 ...
- 像Java一样管理对象:T&形式仅仅用在参数传递
类的对象为了关联/包含一个T类型的instance,若成员变量包括T*/ T&, 这种设计叫做“aggregation”(聚合):而若采用T 形式,则称为"composition&q ...
- spark内存管理器--MemoryManager源码解析
MemoryManager内存管理器 内存管理器可以说是spark内核中最重要的基础模块之一,shuffle时的排序,rdd缓存,展开内存,广播变量,Task运行结果的存储等等,凡是需要使用内存的地方 ...
- 【案例】保健品行业如何优化供应链管理?APS系统来帮忙
仙乐健康一直致力于为了客户提供世界级的产品及服务,随着业务量的不断扩大,公司先后实施了ERP系统,CRM系统,WMS系统,OA系统,朝着行业信息化水平领先的目标迈进. 但近年仅仅拥有传统ERP系统和手 ...
- Vue 中 $nextTick() 的应用
Vue 在更新 DOM 时是异步执行的. 只要侦听到数据变化,Vue 将开启一个队列,并缓冲在同一事件循环中发生的所有数据变更.如果同一个 watcher 被多次触发,只会被推入到队列中一次.这种在缓 ...
- 基于 Vue + Element 的响应式后台模板
项目地址 https://github.com/caochangkui/vue-element-responsive-demo 主要功能 响应式侧边栏 面包屑导航(结合router.js) 路由动效 ...