Skip to content

Commit

Permalink
Create: 0206-reverse-linked-list.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
NikSWE committed Jan 6, 2023
1 parent bc10660 commit 3e19117
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dart/0206-reverse-linked-list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity: O(n)
// Space Complexity: O(1)

class Solution {
ListNode? reverseList(ListNode? head) {
if (head == null) return null;

ListNode? newHead;

while (head != null) {
if (newHead == null) {
newHead = head;
head = head.next;
newHead.next = null;
} else {
var temp = newHead;
newHead = head;
head = head.next;
newHead.next = temp;
}
}

return newHead;
}
}

0 comments on commit 3e19117

Please sign in to comment.