问题

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

  1. public class Program {
  2. public static void Main(string[] args) {
  3. var head = new ListNode(1) {
  4. next = new ListNode(2) {
  5. next = new ListNode(3) {
  6. next = new ListNode(4) {
  7. next = new ListNode(5)
  8. }
  9. }
  10. }
  11. };
  12. var res = ReverseList(head);
  13. ShowArray(res);
  14. head = new ListNode(10) {
  15. next = new ListNode(7) {
  16. next = new ListNode(23) {
  17. next = new ListNode(86) {
  18. next = new ListNode(56)
  19. }
  20. }
  21. }
  22. };
  23. res = ReverseList2(head);
  24. ShowArray(res);
  25. Console.ReadKey();
  26. }
  27. private static void ShowArray(ListNode list) {
  28. var node = list;
  29. while(node != null) {
  30. Console.Write($"{node.val} ");
  31. node = node.next;
  32. }
  33. Console.WriteLine();
  34. }
  35. private static ListNode ReverseList(ListNode head) {
  36. var node = head;
  37. ListNode pre = null;
  38. while(node != null) {
  39. var temp = node.next;
  40. node.next = pre;
  41. pre = node;
  42. node = temp;
  43. }
  44. return pre;
  45. }
  46. private static ListNode ReverseList2(ListNode head) {
  47. if(head == null || head.next == null) return head;
  48. var result = ReverseList2(head.next);
  49. head.next.next = head;
  50. head.next = null;
  51. return result;
  52. }
  53. public class ListNode {
  54. public int val;
  55. public ListNode next;
  56. public ListNode(int x) { val = x; }
  57. }
  58. }

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

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

  1. 5 4 3 2 1
  2. 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. 如何在项目中封装api

    一般在项目中,会有很多的api请求,无论在vue,angular,还是react中都应该把接口封装起来,方便后期的维护. 1.新建一个api文件 我们可以在项目的分目录下创建一个api文件夹,在这里面 ...

  2. 关于ES6的let和const

    变量 var存在的问题 可以重复声明 无法限制修改 没有块级作用域 (在全局范围内有效) 存在变量提升 const/let 不可以重复声明 let a = 1; let a = 2; var b = ...

  3. 面试题五十四:二叉搜索树的第K大节点

    方法:搜索二叉树的特点就是左树小于节点,节点小于右树,所以采用中序遍历法就可以得到排序序列 BinaryTreeNode KthNode(BinaryTreeNode pNode ,int k){ i ...

  4. BUUCTF-web ZJCTF,不过如此

    很明显要利用伪协议读next.php base64解码后查看源码 <?php $id = $_GET['id']; $_SESSION['id'] = $id; function complex ...

  5. hdu6755 Mow

    半平面交+数组模拟双端队列 人生第一次代码过两百行啊...加油加油 #include<iostream> #include<algorithm> #include<cma ...

  6. vb教程图文并茂

    https://blog.csdn.net/baimafujinji/article/details/70198953

  7. 构建私有的verdaccio npm服务

    用了很长一段时间的cnpmjs做库私有库,发现两个问题 1. 最开始是mysql对表情emoij的支持不好,但由于数据库没办法调整所以只好把第三方库都清掉,只留私有库 2. mac 上面cnpm in ...

  8. 数字转字符串&&字符串转数字

    一开始写错了呜呜呜 先是<< 再是>>

  9. 图解 JVM 核心知识点(面试版)

    一.基本概念 1.1 OpenJDK 自 1996 年 JDK 1.0 发布以来,Sun 公司在大版本上发行了 JDK 1.1.JDK 1.2.JDK 1.3.JDK 1.4.JDK 5,JDK 6 ...

  10. 面试官你好,我已经掌握了MySQL主从配置和读写分离,你看我还有机会吗?

    我是风筝,公众号「古时的风筝」,一个简单的程序员鼓励师. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 面试官:我看你简历上写的你们公司数据库是 ...