反转链表 leetcode206

方法1 循环

public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode now = head;
while (now.next != null) {
ListNode temp = now.next;
now.next = now.next.next;
temp.next = head;
head = temp;
}
return head;
}

方法2 递归

public ListNode reverseList2(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = reverseList2(head.next);
ListNode now =newHead;
while (now.next != null) {
now = now.next;
}
now.next = head;
head.next = null;
return newHead;
}

leetcode206 反转链表 两种做法(循环,递归)的更多相关文章

  1. leetcode234 回文链表 两种做法(stack(空间非O(1)),空间O(1))

    link: leetcode234 回文链表 方法1, 快慢指针,把前半部分存入栈中和后半部分比较 public boolean isPalindrome(ListNode head) { if(he ...

  2. jqGrid中实现radiobutton的两种做法

    http://blog.sina.com.cn/s/blog_4f925fc30102e27j.html   jqGrid中实现radiobutton的两种做法 ------------------- ...

  3. SqlServer保留几位小数的两种做法

    SqlServer保留几位小数的两种做法   数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 解决: 1. 使用 Round( ...

  4. js中两种for循环的使用

    针对两种for循环的使用 1. for in循环的使用环境     可用在字符串.数组.对象中, 需注意:其中遍历对象得到的是每个key  的value值  2. for 变量递加的方式        ...

  5. 2021字节跳动校招秋招算法面试真题解题报告--leetcode206 反转链表,内含7种语言答案

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

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

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

  7. PHP实现无限极分类的两种方式,递归和引用

    面试的时候被问到无限极分类的设计和实现,比较常见的做法是在建表的时候,增加一个PID字段用来区别自己所属的分类 $array = array( array('id' => 1, 'pid' =& ...

  8. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

  9. LVS+keepalived 的DR模式的两种做法

    LVS DR模式搭建 准备工作 三台机器: dr:192.168.13.15 rs1:192.168.13.16 rs2: 192.168.13.17 vip:192.168.13.100 修改DR上 ...

随机推荐

  1. idea启动,mysql连接超时错误

    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received ...

  2. 【AST篇】教你如何编写 Eslint 插件

    前言 虽然现在已经有很多实用的 ESLint 插件了,但随着项目不断迭代发展,你可能会遇到已有 ESLint 插件不能满足现在团队开发的情况.这时候,你需要自己来创建一个 ESLint 插件. 本文我 ...

  3. c#委托(Delegates)--基本概念及使用

    在我这菜鸟理解上,委托就是可以用方法名调用另一方法的便捷方法,可以简化switch等语句的重复.最近做项目的时候恰好需要用到委托,便来复习及学习委托的使用.嗯...本人以前并没有用过,只是稍微知道而已 ...

  4. python 绘制对象检测框及中文信息标注

    # 坐标顺序: 上->左->下->右 def draw_bounding_box_on_image(image, ymin, xmin, ymax, xmax, color='red ...

  5. iOS蓝牙4.0开发

    文/starfox寒流(简书作者)原文链接:http://www.jianshu.com/p/974d165f78b5著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. iOS 蓝牙4.0 ...

  6. Laravel5如何向闭合函数内传递参数 where function 传参

    如上,怎么将$title 像$query一样,在函数内部使用? $result = UserMenus::with(['menu'=>function($query){ $query->w ...

  7. 【洛谷P1490】买蛋糕

    题目大意:给定一个正整数 N,求至少从 [1,N] 中选出多少个数能够表示出 [1,N] 中的所有整数,每个数只能被选 1 次,并求出对于最优解有多少种不同的选择方案. 题解:好题. 仅考虑用最少的不 ...

  8. java集合源码分析几篇文章

    java集合源码解析https://blog.csdn.net/ns_code/article/category/2362915

  9. 一种循环C字符数组的骚操作

    #include <stdio.h> #include <stdlib.h> int main() { char wenwa[] = "程劲小盆友在做什么" ...

  10. Docker(3)--常用命令

    1.docker -h 帮助 2.获取镜像 docker pull NAME[:TAG] [root@node3 ~]#docker pull centos:latest 3.启动Container盒 ...