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,这遇到白板编 ...
随机推荐
- IntelliJ IDEA最新破解版2018.3.1(附2018.2.2 完美破解教程)
2018.3.1最新版破解 1.官网下载IDEA 2018.3.1的商业版本点我去下载 2.破解jar下载:JetbrainsIdesCrack-3.4-release-enc.jar点我去下载 3. ...
- MQTT之Mosquitto
https://mosquitto.org/ Eclipse Mosquitto是一个开源(EPL / EDL许可)消息代理,它实现了MQTT协议版本3.1和3.1.1.Mosquitto重量轻,适用 ...
- Rabin-Karp ACM训练
求解问题 寻找S中T出现的位置或次数.假设S的长度为n, T的长度为m, 通过枚举S长度为m的字串的hash值与T的hash值比较.此时使用滚动hash的优化使复杂度不为O(mn). 算法说明 滚动h ...
- CTF最简单的Web题
http://www.shiyanbar.com/ctf/1810 天网管理系统天网你敢来挑战嘛格式:ctf{ }解题链接: http://ctf5.shiyanbar.com/10/web1 查看源 ...
- 实现webservice过滤器,请求日志和权限等
过滤webservice的请求日志,做权限验证功能等. 1. namespace WebApplication1 { public class SimpleWSInvokeMonitorExtensi ...
- session源码剖析
session机制采用的是一种在客户端与服务端之间保持状态的解决方案,由于采用服务器端保持状态的方案在客户端也要保存标识,session机制也要借助于cookie机制达到目的.session保存了客户 ...
- cadence网络表解读及导入
绘制完成原理图,并且通过了DRC检验之后,需要创建和导入网络表,下面网络表内容做简单总结.
- error: can't copy 'docx\templates\default-docx-template': doesn't exist or not a regular file --------------- Failed building wheel for python-docx; python-docx的安装使用;python操作word
本人第一安装python-docx很不幸就出现了,如下的错误:(如果你也遇到同样的错误,不要慌可以参考下面解决方案,由于第一次处理这种错误,如有不对欢迎大家多多批评指正) 问题所在是因为我们的setu ...
- Ubunto使用 码云 创建项目
1.安装 git sudo apt-get install git配置 git 文件 git config --global user.name "你的用户名" git confi ...
- js高级3
1.解决函数内this的指向 可以在函数外提前声明变量_this/that=this 通过apply和call来修改函数内的this指向 (1)二者区别 用法是一样的,就是参数形式不一样 ...