问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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(17)

    CLIENT SIDE ATTACKS - Backdooring exe' s Download an executable file first. VEIL - FRAMEWORK A backd ...

  2. scratch编程体感游戏

    体感游戏有很多种,最常见的就是摄像头和声控了,今天我们要用scratch编写一系列的体感游戏!!!是不是很激动呢? 首先我们来编摄像头类的: No.1拳头打幽灵 挥动头就能打到幽灵了哟! 具体程序如下 ...

  3. 面试题千变万化,为什么总是会问MySQL?

    前言 当你简历上写了 熟悉mysql关系型数据库时,那肯定免不了面试官对于myql索引.事务,慢查询等等的考察 那么到底什么是索引,索引的数据类型有哪些,它们的优缺点以及如何去排查那些慢SQL语句等, ...

  4. java实现单链表的增删改以及排序

    使用java代码模拟单链表的增删改以及排序功能 代码如下: package com.seizedays.linked_list; public class SingleLinkedListDemo { ...

  5. Windows10系统,截图黑屏,怎么办?

    问题:Windows10系统,截图黑屏,怎么办? 图片描述: 原因:也许有  媒体播放软件和系统(或者正在使用的截图软件)起了冲突. 我就开了个这个软件,就完蛋了. 导致了  系统自带的  这两个截图 ...

  6. 深入理解JVM(③)Java的锁优化

    前言 从JDK5到JDK6HotSpot虚拟机开发团队花费了大量的资源实现了各种锁优化技术,如适应性自旋(Adaptive Spinning).锁消除(Lock Elimination).锁膨胀(Lo ...

  7. date 常用格式化输出

    date "+%Y-%m-%d" 2013-02-19 date "+%H:%M:%S" 13:13:59 date "+%Y-%m-%d %H:%M ...

  8. 【laravel】用户认证之----手动认证用户

    模型 如果某个模型类需要用于认证,必须继承自 Illuminate\Foundation\Auth\User 基类,否则会报错.然后在这个模型类中使用 Notifiable Trait,里面提供了用户 ...

  9. 为PhpStorm添加Laravel 代码智能提示功能

    php artisan clear-compiled //清除bootstrap/compiled.php php artisan ide-helper:generate //为 Facades 生成 ...

  10. vector 赋初始值的问题

    这个,输出为1 这个,啥都输不出来. 据说是因为没有初始化. 其实我搜了一下 vector<vector<int> > A;//正确的定义方式 vector<vector ...