forked from liuyubobobo/Play-with-Algorithm-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2.java
More file actions
31 lines (25 loc) · 865 Bytes
/
Copy pathSolution2.java
File metadata and controls
31 lines (25 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 206. Reverse Linked List
// https://leetcode.com/problems/reverse-linked-list/description/
//
// 递归的方式反转链表
// 时间复杂度: O(n)
// 空间复杂度: O(n) - 注意,递归是占用空间的,占用空间的大小和递归深度成正比:)
public class Solution2 {
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode reverseList(ListNode head) {
// 递归终止条件
if(head == null|| head.next == null)
return head;
ListNode rhead = reverseList(head.next);
// head->next此刻指向head后面的链表的尾节点
// head->next->next = head把head节点放在了尾部
head.next.next = head;
head.next = null;
return rhead;
}
}