-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day_3.dart
34 lines (31 loc) · 794 Bytes
/
Day_3.dart
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
32
33
34
class ListNode {
int val;
ListNode? next;
ListNode([this.val = 0, this.next]);
}
class Day3Solution {
///Todo :: 1. Problem --> Merge Two Sorted Lists
ListNode? mergeTwoLists(ListNode? list1, ListNode? list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;
if (list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
} else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
///Todo :: 2. Solution --> Reverse Linked List
ListNode? reverseList(ListNode? head) {
ListNode? prev = null;
ListNode? curr = head;
while(curr !=null) {
ListNode? next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}