Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow 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的更多相关文章

  1. leetcode 206. Reverse Linked List(剑指offer16)、

    206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...

  2. 链表 206 Reverse Linked List, 92,86, 328, 2, 445

    表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...

  3. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  4. 206. Reverse Linked List - LeetCode

    Question 206. Reverse Linked List Solution 题目大意:对一个链表进行反转 思路: Java实现: public ListNode reverseList(Li ...

  5. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  6. 【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 ...

  7. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  8. [LeetCode] 206. Reverse Linked List ☆(反转链表)

    Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3-> ...

  9. [刷题] 206 Reverse Linked List

    要求 反转一个链表 不得改变节点的值 示例 head->1->2->3->4->5->NULL NULL<-1<-2<-3<-4<-5 ...

随机推荐

  1. Sitecore 内容版本设计

    Sitecore内容变化的跟踪显着偏离既定规范.了解Sitecore中版本控制和工作流程的细节,该产品是对这些发布工具的回答. 在出版界,实时跟踪内容变化很常见,可能是由于Microsoft Word ...

  2. - Java中boolean类型占用多少个字节 MD

    目录 目录 Java中boolean类型占用多少个字节 1个bit(1位) 1个Byte(1字节,8位) 4个Byte(4字节,32位) 分析 官方文档中的描述 Markdown版本笔记 我的GitH ...

  3. 基于Mybatis-Plus实现自动化操作创建时间和修改时间

    引入 在实际开发中,总会避免不了操作数据库,而在数据库中每个表都会有create_time和update_time字段记录操作时间,我们在操作这两个时间的时候也可能会出现不一致的情况,或者说这两个字段 ...

  4. C# zip压缩 Ionic.Zip.dll

    #region Ionic.Zip压缩文件 //压缩方法一 public void ExeCompOne() { string FileName = DateTime.Now.ToString(&qu ...

  5. MAC OS系统替换brew.npm, pip 使用阿里云的镜像源

    替换brew.git:cd "$(brew --repo)"git remote set-url origin https://mirrors.aliyun.com/homebre ...

  6. Django:RestFramework之-------序列化器

    8.序列化 功能: 对请求数据进行验证 对Queryset进行序列化 8.1一个简单序列化: import json from api import models from rest_framewor ...

  7. [转] Cache 和 Buffer的区别

    程序员开发过程中经常会遇到“缓存”.“缓冲”等相似概念,之前没有特别关注,现在停下来做一下总结,才能更好地前行. 先来下枯燥的概念: 1.Cache:缓存区,是高速缓存,是位于CPU和主内存之间的容量 ...

  8. Android中自定义水球

    如图所示: 自定义属性: 在values下创建attrs.xml 文件 <?xml version="1.0" encoding="utf-8"?> ...

  9. Zebra-打印特殊字符

    Zebra在打印一些特殊的字符时,会出异常. 在要打印的字符串前加  ^FH  然后将字符换成 ASCii编码或utf-8编码的16进制,在前面加_,如D094写成_DO_94 查看字符的编码 htt ...

  10. 将Quartz.NET集成到 Castle中

    原文:https://cloud.tencent.com/developer/article/1030346 Castle是针对.NET平台的一个开源项目,从数据访问框架ORM到IOC容器,再到WEB ...