Skip to content

Commit 8cd97a5

Browse files
committed
leetcode
1 parent d10b113 commit 8cd97a5

File tree

4 files changed

+243
-0
lines changed

4 files changed

+243
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
7575
- [Add One Row to Tree](AddOneRowToTree/add_one_row_to_tree.dart)
7676
- [Time Based Key-Value Store](TimeBasedKeyValueStore/time_based_key_value_store.dart)
7777
- [My Calendar III](MyCalendar-III/my_calendar_III.dart)
78+
- [Reverse Linked List](ReverseLinkedList/reverse_linked_list.dart)
7879

7980
## Reach me via
8081

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
3+
-* Reverse Linked List *-
4+
5+
Given the head of a singly linked list, reverse the list, and return the reversed list.
6+
7+
8+
9+
Example 1:
10+
11+
12+
Input: head = [1,2,3,4,5]
13+
Output: [5,4,3,2,1]
14+
Example 2:
15+
16+
17+
Input: head = [1,2]
18+
Output: [2,1]
19+
Example 3:
20+
21+
Input: head = []
22+
Output: []
23+
24+
25+
Constraints:
26+
27+
The number of nodes in the list is the range [0, 5000].
28+
-5000 <= Node.val <= 5000
29+
30+
31+
Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?
32+
33+
34+
*/
35+
36+
// Definition for singly-linked list.
37+
38+
import 'dart:collection';
39+
40+
class ListNode {
41+
int val;
42+
ListNode? next;
43+
ListNode([this.val = 0, this.next]);
44+
}
45+
46+
class A {
47+
// Iterative- O(n)
48+
// Runtime: 322 ms, faster than 95.89% of Dart online submissions for Reverse Linked List.
49+
// Memory Usage: 156.1 MB, less than 15.07% of Dart online submissions for Reverse Linked List.
50+
51+
ListNode? reverseList(ListNode? head) {
52+
// previous value is null because we donn't know if it exists of not
53+
ListNode? previous = null;
54+
// we are at current value mean the first value head
55+
ListNode? current = head;
56+
// we don't know if there is next value or not so null
57+
ListNode? next = null;
58+
// if we assume there is a value at the start of the list
59+
while (current != null) {
60+
// storing the next element
61+
next = current.next;
62+
// we changing the next of the current value - reversing
63+
current.next = previous;
64+
// moving the previous and current on step forward
65+
previous = current;
66+
current = next;
67+
}
68+
head = previous;
69+
return head;
70+
}
71+
}
72+
73+
class B {
74+
// Recursive
75+
// Runtime: 377 ms, faster than 78.08% of Dart online submissions for Reverse Linked List.
76+
// Memory Usage: 142.7 MB, less than 60.27% of Dart online submissions for Reverse Linked List.
77+
ListNode? reverseList(ListNode? head) {
78+
if (head == null || head.next == null) return head;
79+
// reversing the rest list and put the fist element at the end
80+
ListNode? rest = reverseList(head.next);
81+
// linking the rest of the list to head
82+
head.next?.next = head;
83+
head.next = null;
84+
// fixing the hed pointer
85+
return rest;
86+
}
87+
}
88+
89+
class D {
90+
ListNode? reverseList(ListNode? head) {
91+
Queue<ListNode> st = Queue();
92+
ListNode listNode = ListNode(0);
93+
ListNode? temp = listNode;
94+
while (head != null) {
95+
st.add(head);
96+
head = head.next;
97+
}
98+
while (st.isNotEmpty) {
99+
temp?.next = st.removeLast();
100+
temp = temp?.next;
101+
}
102+
temp?.next = null;
103+
return listNode.next;
104+
}
105+
}
106+
107+
class E {
108+
// Runtime: 319 ms, faster than 98.63% of Dart online submissions for Reverse Linked List.
109+
// Memory Usage: 161.1 MB, less than 5.48% of Dart online submissions for Reverse Linked List.
110+
ListNode? reverseList(ListNode? head) {
111+
if (head == null || head.next == null) return head;
112+
113+
Queue<ListNode> stack = Queue();
114+
while (head != null) {
115+
stack.add(head);
116+
head = head.next;
117+
}
118+
119+
ListNode? nHead = stack.last;
120+
ListNode? p;
121+
while (stack.isNotEmpty) {
122+
p = stack.removeLast();
123+
124+
if (stack.isNotEmpty) {
125+
ListNode? pre = stack.last;
126+
p.next = pre;
127+
} else
128+
p.next = null;
129+
}
130+
return nHead;
131+
}
132+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func reverseList(head *ListNode) *ListNode {
10+
// Runtime: 0 ms, faster than 100.00% of Go online submissions for Reverse Linked List.
11+
// Memory Usage: 2.6 MB, less than 77.80% of Go online submissions for Reverse Linked List.
12+
var previous, current *ListNode = nil, head
13+
for current != nil {
14+
previous, current, current.Next = current, current.Next, previous
15+
}
16+
return previous
17+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 🔥 Reverse Linked List 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Definition for singly-linked list
4+
5+
```dart
6+
class ListNode {
7+
int val;
8+
ListNode? next;
9+
ListNode([this.val = 0, this.next]);
10+
}
11+
```
12+
13+
## Solution - 1 Iterative O(n)
14+
15+
```dart
16+
class Solution {
17+
// Runtime: 322 ms, faster than 95.89% of Dart online submissions for Reverse Linked List.
18+
// Memory Usage: 156.1 MB, less than 15.07% of Dart online submissions for Reverse Linked List.
19+
20+
ListNode? reverseList(ListNode? head) {
21+
// previous value is null because we donn't know if it exists of not
22+
ListNode? previous = null;
23+
// we are at current value mean the first value head
24+
ListNode? current = head;
25+
// we don't know if there is next value or not so null
26+
ListNode? next = null;
27+
// if we assume there is a value at the start of the list
28+
while (current != null) {
29+
// storing the next element
30+
next = current.next;
31+
// we changing the next of the current value - reversing
32+
current.next = previous;
33+
// moving the previous and current on step forward
34+
previous = current;
35+
current = next;
36+
}
37+
head = previous;
38+
return head;
39+
}
40+
}
41+
```
42+
43+
## Solution - 2 Recursive O(n)
44+
45+
```dart
46+
class Solution {
47+
// Runtime: 377 ms, faster than 78.08% of Dart online submissions for Reverse Linked List.
48+
// Memory Usage: 142.7 MB, less than 60.27% of Dart online submissions for Reverse Linked List.
49+
ListNode? reverseList(ListNode? head) {
50+
if (head == null || head.next == null) return head;
51+
// reversing the rest list and put the fist element at the end
52+
ListNode? rest = reverseList(head.next);
53+
// linking the rest of the list to head
54+
head.next?.next = head;
55+
head.next = null;
56+
// fixing the hed pointer
57+
return rest;
58+
}
59+
}
60+
```
61+
62+
## Solution - 3 Don't Panic := Using Queue as STACK
63+
64+
```dart
65+
import 'dart:collection';
66+
67+
class Solution {
68+
// Runtime: 319 ms, faster than 98.63% of Dart online submissions for Reverse Linked List.
69+
// Memory Usage: 161.1 MB, less than 5.48% of Dart online submissions for Reverse Linked List.
70+
ListNode? reverseList(ListNode? head) {
71+
if (head == null || head.next == null) return head;
72+
73+
Queue<ListNode> stack = Queue();
74+
while (head != null) {
75+
stack.add(head);
76+
head = head.next;
77+
}
78+
79+
ListNode? nHead = stack.last;
80+
ListNode? p;
81+
while (stack.isNotEmpty) {
82+
p = stack.removeLast();
83+
84+
if (stack.isNotEmpty) {
85+
ListNode? pre = stack.last;
86+
p.next = pre;
87+
} else
88+
p.next = null;
89+
}
90+
return nHead;
91+
}
92+
}
93+
```

0 commit comments

Comments
 (0)