Skip to content

Commit 6271ccb

Browse files
authored
reverse linked list solution
1 parent a2976d0 commit 6271ccb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

reverse-linked-list/yhkee0404.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode? next;
6+
* ListNode([this.val = 0, this.next]);
7+
* }
8+
*/
9+
class Solution {
10+
ListNode? reverseList(ListNode? head) {
11+
var u = null; // S(n) = O(1)
12+
while (head != null) { // T(n) = O(n)
13+
final temp = head.next;
14+
head.next = u;
15+
u = head;
16+
head = temp;
17+
}
18+
return u;
19+
}
20+
}

0 commit comments

Comments
 (0)