代码拉取完成,页面将自动刷新
同步操作将从 程序员二师兄/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/**
* @创建人 luoxiang
* @创建时间 2019/9/25 11:00
* @描述 206. 反转链表 https://leetcode-cn.com/problems/reverse-linked-list/
*/
public class LeetCode_206 {
public static void main(String[] args) {
LeetCode_206 code206 = new LeetCode_206();
ListNode node = new ListNode(1);
node.next = new ListNode(2);
node.next.next = new ListNode(3);
node.next.next.next = new ListNode(4);
node.next.next.next.next = new ListNode(5);
ListNode list = code206.reverseList(node);
while (list != null) {
System.out.print(list.val + " -> ");
list = list.next;
}
}
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode tmp = reverseList(head.next);
head.next.next = head;
head.next = null;
return tmp;
}
/**
* Method 1
* Created by LuoXiang on 2019/09/25 11:05
* Desc: 思路 - 两条链表的反转操作
* 复杂度: 时间复杂度:O(n) ; 空间复杂度:O(1)
**/
public ListNode reverseList1(ListNode head) {
ListNode pre = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = pre;
pre = curr;
curr = nextTemp;
}
return pre;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。