leetcode206 反转链表 两种做法(循环,递归)
2024-10-07 05:26:09
方法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;
}
最新文章
- November 3rd Week 45th Thursday 2016
- PHP网页缓存技术
- Java for LeetCode 206 Reverse Linked List
- Python内置模块(2)
- win7录屏工具
- Java多线程之join
- Server Profiler
- AndroidRichText 让Textview轻松的支持富文本(图像ImageSpan、点击效果等等类似QQ微信聊天)
- [置顶] android 图片库的封装
- 安装Fedora(附镜像下载地址)
- mvn 使用中的错误
- bzoj2019 [Usaco2009 Nov]找工作
- 实现多条件模糊查询SQL语句
- .NET DateTime 源码学习
- 【爆料】-《南澳大学毕业证书》UniSA一模一样原件
- 【Ansible 文档】【译文】入门教程
- 【Spring学习笔记-MVC-10】Spring MVC之数据校验
- 深入理解MyBatis的原理:整个体系
- 使用Editplus和Dev C++配置C++的编译运行环 境
- iOS开发- 速学Swift-中文概述