[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 ...
随机推荐
- How to let your website login with domain account when using IIS to deploy it?
如何让你的网站以域账号登录 Select your website in IIS Manager, open Authentication, enable Windows Authentication ...
- SpringBoot使用@ServerEndpoint无法依赖注入问题解决(WebSocket)
如上两图所示,在WebSocket中我想使用Redis.把自己编写的RedisUtil使用@Autowired自动注入到当前类. 在运行时,出现异常:java.lang.NullPointExcept ...
- STM8L独立看门狗IWDG
简单扼要 void IWDG_Init(void)//初始化 { IWDG->KR = 0xcc;//启动IWDG IWDG->KR = 0x55;//解除PR及RLR的写保护 IWDG- ...
- 上传文件时用form.submit提交的时候在低版本的IE中报拒绝访问的错误
上传文件的时候,在IE7下总是传不了,但FireFox,IE11和Chrome下则可以上传.发现是form.submit();时出错了(“拒绝访问”). html代码为: <label oncl ...
- asp.net core 系列之允许跨域访问2之测试跨域(Enable Cross-Origin Requests:CORS)
这一节主要讲如何测试跨域问题 你可以直接在官网下载示例代码,也可以自己写,我这里直接使用官网样例进行演示 样例代码下载: Cors 一.提供服务方,这里使用的是API 1.创建一个API项目.或者直接 ...
- Identity和IdentityServer的区别及联系
关于Identity和IdentityServer初学的时候可能会有一些疑惑(虽然我也不是很精深吧),但是,这里说一下自己关于这两者的一些理解,如有错误,欢迎指正 总体上, ASP.NET Core ...
- 网络编程中用到的SOCKET是什么?
摘取网络上的一些内容: 什么是TCP/IP.UDP? Socket在哪里呢? Socket是什么呢? 什么是TCP/IP.UDP? TCP/IP(Transmission Control Protoc ...
- C# vb .net图像合成-合成自定义路径
在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...
- 关于 返回数据类型 后 加& 的作用
关于 返回数据类型 后 加& 的作用 请看以下图片 我的测试 //纯粹就是 为了 可作为 左值而存在的 方法 前 的返回值 int& ,如 int& Method(); // ...
- vue/react/angular开发的css架构思考
前端开发现在已经从传统的后端web多页面开发模式转向前端单页SPA开发模式,而vuejs/react/angular则是开发SPA非常优秀的前端框架.组件化开发由react最早提出,vuejs后发优势 ...