leetcode — reverse-linked-list
/**
* Source : https://leetcode.com/problems/reverse-linked-list/
*
*
* Reverse a singly linked list.
*
* click to show more hints.
*
* Hint:
* A linked list can be reversed either iteratively or recursively. Could you implement both?
*/
public class ReverseLinkedList {
/**
*
* 反转单向链表
*
* 使用迭代的方法
*
* @param head
* @return
*/
public Node reverseByIterative (Node head) {
Node pre = null;
while (head != null) {
Node next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
/**
* 使用递归
*
* @param head
* @return
*/
public Node reverseByRecursion (Node head, Node pre) {
if (head == null) {
return head;
}
if (head.next == null) {
head.next = pre;
return head;
}
Node next = head.next;
head.next = pre;
pre = head;
return reverseByRecursion(next, pre);
}
private static class Node {
int value;
Node next;
@Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "" : next.value) +
'}';
}
}
private static void print (Node node) {
while (node != null) {
System.out.println(node);
node = node.next;
}
System.out.println();
}
public Node createList (int[] arr) {
if (arr.length == 0) {
return null;
}
Node head = new Node();
head.value = arr[0];
Node pointer = head;
for (int i = 1; i < arr.length; i++) {
Node node = new Node();
node.value = arr[i];
pointer.next = node;
pointer = pointer.next;
}
return head;
}
public static void main(String[] args) {
ReverseLinkedList reverseLinkedList = new ReverseLinkedList();
print(reverseLinkedList.reverseByIterative(reverseLinkedList.createList(new int[]{1,2,3,4})));
print(reverseLinkedList.reverseByIterative(reverseLinkedList.createList(new int[]{})));
print(reverseLinkedList.reverseByRecursion(reverseLinkedList.createList(new int[]{1,2,3,4}), null));
print(reverseLinkedList.reverseByRecursion(reverseLinkedList.createList(new int[]{}), null));
}
}
leetcode — reverse-linked-list的更多相关文章
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)
题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...
- [leetcode]Reverse Linked List II @ Python
原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...
- [LeetCode] Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- [LeetCode] Reverse Linked List
Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...
- (leetcode)Reverse Linked List 脑子已经僵住
Reverse a singly linked list. 参考http://www.2cto.com/kf/201110/106607.html 方法1: 讲每个节点的指针指向前面就可以. /** ...
- [Leetcode] Reverse linked list ii 反转链表
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...
- leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
- 翻转单链表 leetcode Reverse Linked List
翻转一个单链表.这个题目听说很多次了,总感觉肯定不是什么难题. 现在真的有点好高骛远了!总感觉那种很难的算法题才是难题,这种题没必要做.其实眼高手低啊. 这种easy题,我都不能一遍ac,这遇到白板编 ...
随机推荐
- 1064 Financial Management
http://acm.hdu.edu.cn/showproblem.php?pid=1064 思路:看懂英文就很简单,就是12个数相加求平均数就ok了. 扩展: C++ 标准输入输出流的控制符 #in ...
- CentOS7部分调优命令
一般CentOS的磁盘空间占用最大的就是日志文件,日志文件主要保存在./log目录里,因此通过以下命令可以检查目录的大小. du -ah --max-depth=1 du命令的一些常用参数: -a或- ...
- 一个陌生女人的来信 Brief einer Unbekannten
[Österreich] Stefan Zweig Mein Kind ist gestorben, unser Kind - jetzt habe ich niemanden mehr in der ...
- SpringBoot报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
解决方法: 在数据库连接url配置后边加&serverTimezone=GMT%2B8 例: jdbc:mysql://127.0.0.1:3306/test改为jdbc:mysql://12 ...
- SpringBoot+ Mybatis 搭建
spring boot+mybatis整合 LZ今天自己搭建了下Spring boot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多.其实只用Spring bo ...
- hadoop环境搭建及Wordcount案例实验
1.Linux环境变量设置 在/etc/profile中添加环境变量 sudo vim /etc/profile PATH=$PATH:/usr/local/hadoop/bin source /et ...
- OpenCV常用头文件介绍
转载:https://www.cnblogs.com/wangguchao/p/7244483.html 1.OpenCV包含的模块 cv – 核心函数库 cvaux – 辅助函数库 cxcore – ...
- ucore代码分析
lab2: 总共分为四个包一个文件,分别为: boot: 操作系统加载程序代码 kern: 操作系统内核代码 libs: 相关的库和数据结构 tools: 相关编译链接调试工具 Makefile: 构 ...
- linux sqlite3 相关
数据库sqlite 1 用下载好的安装包安装 linux@ubuntu:~/sqlite3$ sudo dpkg -i libsqlite3-0_3.7.2-1ubuntu0.1_i386_1.deb ...
- [Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...