Skip to content

Commit e4de9ae

Browse files
committed
[week2] solve 206. Reverse Linked List
1 parent 17b7837 commit e4de9ae

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

reverse-linked-list/bky373.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/reverse-linked-list/
3+
* TC: O(N)
4+
* SC: O(N)
5+
*/
6+
class Solution_206 {
7+
8+
public ListNode reverseList(ListNode head) {
9+
if (head == null) {
10+
return head;
11+
}
12+
ListNode a = new ListNode(head.val);
13+
ListNode b;
14+
while (head.next != null) {
15+
b = new ListNode(head.next.val, a);
16+
a = b;
17+
head = head.next;
18+
}
19+
return a;
20+
}
21+
}

0 commit comments

Comments
 (0)