问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3828 访问。

反转一个单链表。

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

进阶:

你可以迭代或递归地反转链表。你能否用两种方法解决这道题?


Reverse a singly linked list.

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?


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3828 访问。

public class Program {

    public static void Main(string[] args) {
var head = new ListNode(1) {
next = new ListNode(2) {
next = new ListNode(3) {
next = new ListNode(4) {
next = new ListNode(5)
}
}
}
}; var res = ReverseList(head);
ShowArray(res); head = new ListNode(10) {
next = new ListNode(7) {
next = new ListNode(23) {
next = new ListNode(86) {
next = new ListNode(56)
}
}
}
}; res = ReverseList2(head);
ShowArray(res); Console.ReadKey();
} private static void ShowArray(ListNode list) {
var node = list;
while(node != null) {
Console.Write($"{node.val} ");
node = node.next;
}
Console.WriteLine();
} private static ListNode ReverseList(ListNode head) {
var node = head;
ListNode pre = null;
while(node != null) {
var temp = node.next;
node.next = pre;
pre = node;
node = temp;
}
return pre;
} private static ListNode ReverseList2(ListNode head) {
if(head == null || head.next == null) return head;
var result = ReverseList2(head.next);
head.next.next = head;
head.next = null;
return result;
} public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3828 访问。

5 4 3 2 1
56 86 23 7 10

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#206-反转链表(Reverse Linked List)的更多相关文章

  1. LeetCode 206. 反转链表(Reverse Linked List) 16

    206. 反转链表 206. Reverse Linked List 题目描述 反转一个单链表. 每日一算法2019/5/19Day 16LeetCode206. Reverse Linked Lis ...

  2. C#LeetCode刷题之#141-环形链表(Linked List Cycle)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3901 访问. 给定一个链表,判断链表中是否有环. 进阶: 你能否 ...

  3. leetcode 206 反转链表 Reverse Linked List

    C++解法一:迭代法,使用前驱指针pre,当前指针cur,临时后继指针nxt: /** * Definition for singly-linked list. * struct ListNode { ...

  4. Leetcode春季打卡活动 第二题:206. 反转链表

    Leetcode春季打卡活动 第二题:206. 反转链表 206. 反转链表 Talk is cheap . Show me the code . /** * Definition for singl ...

  5. LeetCode刷题总结-栈、链表、堆和队列篇

    本文介绍LeetCode上有关栈.链表.堆和队列相关的算法题的考点,推荐刷题20道.具体考点分类如下图: 一.栈 1.数学问题 题号:85. 最大矩形,难度困难 题号:224. 基本计算器,难度困难 ...

  6. LeetCode 206:反转链表 Reverse Linked List

    反转一个单链表. Reverse a singly linked list. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3- ...

  7. LeetCode刷题 --杂篇 --数组,链表,栈,队列

    武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...

  8. leetcode-解题记录 206. 反转链表

    题目 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶: 你可 ...

  9. [Swift]LeetCode206. 反转链表 | Reverse Linked List

    Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...

随机推荐

  1. Ethical Hacking - GAINING ACCESS(19)

    Client-Side Attacks - Social Engineering Tool: The FAT RAT Just like Veil, it generates Undetectable ...

  2. Go的100天之旅-06数组和Slice

    目录 数组 Slice 数组 Go的数组和其它语言基本上一样,是长度固定的特定类型元素组成的序列,这基本上是所有语言数组的特性.和其它语言相比差异主要在声明和初始化的写法上,下面是简单声明一个数组: ...

  3. Electron-vue 项目搭建

    Electron 应用技术体系推荐 目录结构 demo(项目名称) ├─ .electron-vue(webpack配置文件) │  └─ build.js(生产环境构建代码) | └─ dev-cl ...

  4. Lua中 pairs和ipairs的区别

    Lua系列–pairs和ipairsLua中Table的存储方式在看二者的区别之前,我们首先来看一下Lua中的table是如何在内存中进行分配的.Table的组成:1.哈希表 用来存储Key-Valu ...

  5. 大家是怎么做APP接口的版本控制的?欢迎进来看看我的方案。升级版的Versioning

    背景 APP不同于网站,网站程序一发版,所有用户看到的都是最新的页面.调用最新的接口,没有新老版本一说.APP一旦下载到用户手机上,用户不更新你拿他一点办法都没有,但是随着业务的调整,同一个接口的请求 ...

  6. 线程_使用multiprocessing启动一个子进程及创建Process 的子类

    from multiprocessing import Process import os # 子进程执行的函数 def run_proc(name): print("子进程运行中,名称:% ...

  7. Django学习路3

    1.打开 Data Source alt insert 打开 Data Source 找到 db.sqlite3 确定 Download 下载后 TestConnection 测试是否成功 2.项目下 ...

  8. SELECT from Nobel Tutorial

    02.SELECT from Nobel Tutorial 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.Change the quer ...

  9. PHP array_walk_recursive() 函数

    实例 对数组中的每个元素应用用户自定义函数: <?phpfunction myfunction($value,$key){echo "The key $key has the valu ...

  10. PHP array_values() 函数

    实例 返回数组中所有的值(不保留键名): <?php$a=array("Name"=>"Peter","Age"=>&qu ...