1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc206.java 1.11 KB
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-07-11 11:08 . 20190711
package code;
/*
* 206. Reverse Linked List
* 题意:链表反转
* 难度:Easy
* 分类:Linked List
* 思路:2中方法:设置一个快走一步的快指针,注意赋值操作顺序。还有一种递归的方法。
* Tips:递归的方法有点绕,多看下
* lc25, lc206
*/
public class lc206 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode reverseList(ListNode head) {
ListNode pre = null; //头结点变尾节点,指向null
while(head!=null){
ListNode next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
public ListNode reverseList2(ListNode head) { //递归
return reverseListInt(head, null);
}
private ListNode reverseListInt(ListNode head, ListNode pre) {
if (head == null)
return pre;
ListNode next = head.next;
head.next = pre;
return reverseListInt(next, head); //尾递归,操作已经完成,最后返回最后结果罢了
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891