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,这遇到白板编 ...
随机推荐
- 如何使用 tf object detection
# 如何使用 tf object detection https://juejin.i m/entry/5a7976166fb9a06335319080 https://towardsdatascie ...
- xshell登陆服务器步骤
Xshell远程连接服务器 打开xshell后找到左上角第一个“文件”点击,弹出来一个下拉框,选择“新建”点击(或者直接按下快捷键“Alt+n”). 点击“新建”之后就会出现下面这样一 ...
- SpringBoot报错:Table 'database_name.hibernate_sequence' doesn't exist
引起条件: SpringBoot+JPA插入包含自增字段的对象 @Id @GeneratedValue private Integer id; 解决方法: 给注解添加属性 @Id @Generated ...
- Redis_MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk问题解决
原因:可参考https://www.linuxidc.com/Linux/2012-07/66079.htm 解决方案一: 修改redis.conf中 stop-writes-on-bgsave-er ...
- kettle web化
kettle web化 通过Java API调用kettle核心代码,并基于Spring Boot提供简易的Web管理界面. 背景 在工作中,通过kettle这款ETL产品进行数据处理时,是通过kit ...
- ECharts使用:this.dom.getContext is not a function
echarts 画图报错 this.dom.getContext is not a function; 原因:因为在初始化echarts的时候,echarts.js规定只能使用dom原生方法获取标签, ...
- django(models)视图与html 简单的操作
!数据提前写好 urls映射图 点击a标签之后
- SSIS - 6.序列容器和优先约束
一.多样的优先约束(看例子) 1)打开一个空白的SSIS包,拖拽4个脚本任务到设计面板上,重命名后连接起来,如下图所示. 2)执行包,可以看到任务执行成功.之后我们改变C和D之间的优先约束为“失败”. ...
- 实现CSS隐藏滚动条并可以滚动内容
隐藏滚动条的同时还需要支持滚动,我们经常在前端开发中遇到这种情况,最容易想到的是加一个iscroll插件,但其实现在CSS也可以实现这个功能,我已经在很多地方使用了,下面一起看看这三种方法. 方法1: ...
- VsCode 使用专用编程字体FiraCode
FiraCode资料:https://github.com/tonsky/FiraCode PHP代码效果如下: VsCode 配置中添加: "editor.fontFamily" ...