Skip to content

Commit fe403cd

Browse files
committed
solve reversed-linked-list
1 parent bed992d commit fe403cd

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

reverse-linked-list/hyejj19.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(1)
4+
*/
5+
function reverseList(head: ListNode | null): ListNode | null {
6+
let prev = null;
7+
let cur = head;
8+
let next = null;
9+
10+
while (cur !== null) {
11+
next = cur.next;
12+
cur.next = prev;
13+
prev = cur;
14+
cur = next;
15+
}
16+
17+
return prev;
18+
}

0 commit comments

Comments
 (0)