Skip to content

Commit 64ec550

Browse files
committed
leetcode
1 parent c9ed109 commit 64ec550

File tree

7 files changed

+635
-0
lines changed

7 files changed

+635
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ This repo contain leetcode solution using DART and GO programming language. Most
5050
- [Best Time to Buy and Sell Stock](BestTimeToBuyAndSellStock/best_time_to_buy_and_sell_stock.dart)
5151
- [Valid Palindrome](ValidPalindrome/valid_palindrome.dart)
5252
- [Push Dominoes](PushDominoes/push_dominoes.dart)
53+
- [Single Number](SingleNumber/single_number.dart)
54+
- [Remove Nth Node From End of List](RemoveNthNodeFromEndOfList/remove_nth_node_from_end_of_list.dart)
5355

5456
## Reach me via
5557

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
3+
-* Remove Nth Node From End of List *-
4+
5+
6+
Given the head of a linked list, remove the nth node from the end of the list and return its head.
7+
8+
9+
10+
Example 1:
11+
12+
13+
Input: head = [1,2,3,4,5], n = 2
14+
Output: [1,2,3,5]
15+
Example 2:
16+
17+
Input: head = [1], n = 1
18+
Output: []
19+
Example 3:
20+
21+
Input: head = [1,2], n = 1
22+
Output: [1]
23+
24+
25+
Constraints:
26+
27+
The number of nodes in the list is sz.
28+
1 <= sz <= 30
29+
0 <= Node.val <= 100
30+
1 <= n <= sz
31+
32+
*/
33+
34+
// Definition for singly-linked list.
35+
class ListNode {
36+
int val;
37+
ListNode? next;
38+
ListNode([this.val = 0, this.next]);
39+
}
40+
41+
// Two Pointers
42+
43+
class Solution {
44+
// Runtime: 465 ms, faster than 52.94% of Dart online submissions for Remove Nth Node From End of List.
45+
// Memory Usage: 142.4 MB, less than 88.24% of Dart online submissions for Remove Nth Node From End of List.
46+
ListNode? removeNthFromEnd(ListNode? head, int n) {
47+
// if the head is null we will simply return null
48+
if (head == null) return null;
49+
// Two pointers of our linked list
50+
ListNode? fast = head;
51+
ListNode? slow = head;
52+
// looping through the value and pointing it to the next
53+
for (int i = 1; i <= n; ++i) fast = fast!.next;
54+
// if the previous value is null but next is not
55+
ListNode? prev = null;
56+
// while the first pointer is not null
57+
while (fast != null) {
58+
// fast will hold the next value
59+
fast = fast.next;
60+
// previous will hold the second pointer
61+
prev = slow;
62+
slow = slow!.next;
63+
}
64+
// if previous is null
65+
if (prev == null) {
66+
// head will pint to the next value
67+
head = head.next;
68+
} else {
69+
prev.next = slow!.next;
70+
slow.next = null;
71+
}
72+
// else we will simply return head
73+
return head;
74+
}
75+
}
76+
77+
// Three Pointer
78+
/*
79+
80+
Approach :- First increase the fast pointer by n steps to ensure that when the fast pointer points to null , at the same time, the slow pointer points to the location of the node which is to be removed
81+
fast pointer : to move n steps ahead of slow pointer
82+
slow pointer : this will point to the exact node which is to be removed once the fast pointer reaches null
83+
prev pointer: this will point to the node before the slow pointer because this pointer will bypass the node which is to be removed and get connected directly to the node next to slow.
84+
85+
86+
*/
87+
88+
class B {
89+
// Runtime: 509 ms, faster than 50.00% of Dart online submissions for Remove Nth Node From End of List.
90+
// Memory Usage: 142.5 MB, less than 79.41% of Dart online submissions for Remove Nth Node From End of List.
91+
ListNode? removeNthFromEnd(ListNode? head, int n) {
92+
ListNode? fast = head;
93+
ListNode? slow = head;
94+
ListNode? prev = null;
95+
//Loop to take the fast pointer n steps ahead of the slow pointer.
96+
while (n > 0) {
97+
fast = fast?.next;
98+
n--;
99+
}
100+
//If fast pointer points to null, it means that the node has to be removed from the beginning.
101+
if (fast == null) return slow?.next;
102+
103+
while (fast != null) {
104+
fast = fast.next;
105+
prev = slow;
106+
slow = slow?.next;
107+
}
108+
109+
if (prev != null)
110+
prev.next = slow?.next;
111+
else if (prev == null) return null;
112+
return head;
113+
}
114+
}
115+
116+
class C {
117+
// Runtime: 488 ms, faster than 52.94% of Dart online submissions for Remove Nth Node From End of List.
118+
// Memory Usage: 142.4 MB, less than 79.41% of Dart online submissions for Remove Nth Node From End of List.
119+
ListNode? removeNthFromEnd(ListNode? head, int n) {
120+
ListNode? fast = head, slow = head;
121+
for (int i = 1; i <= n; i++) fast = fast?.next;
122+
// for cases where n = length of linked-list i.e. if first node is to be deleted
123+
if (fast == null) {
124+
head = head?.next;
125+
return head;
126+
}
127+
while (fast?.next != null) {
128+
slow = slow?.next;
129+
fast = fast?.next;
130+
}
131+
slow?.next = slow.next?.next;
132+
return head;
133+
}
134+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func removeNthFromEnd(head *ListNode, n int) *ListNode {
10+
dummy := new(ListNode)
11+
dummy.Next = head
12+
fast := head
13+
slow := dummy
14+
for i := 0; i < n; i++ {
15+
fast = fast.Next
16+
}
17+
18+
for fast != nil {
19+
fast = fast.Next
20+
slow = slow.Next
21+
}
22+
23+
slow.Next = slow.Next.Next
24+
25+
return dummy.Next
26+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# 🔥 DART || 3 Solutions || 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 Two-Pointers
14+
15+
```dart
16+
class Solution {
17+
// Runtime: 465 ms, faster than 52.94% of Dart online submissions for Remove Nth Node From End of List.
18+
// Memory Usage: 142.4 MB, less than 88.24% of Dart online submissions for Remove Nth Node From End of List.
19+
ListNode? removeNthFromEnd(ListNode? head, int n) {
20+
// if the head is null we will simply return null
21+
if (head == null) return null;
22+
// Two pointers of our linked list
23+
ListNode? fast = head;
24+
ListNode? slow = head;
25+
// looping through the value and pointing it to the next
26+
for (int i = 1; i <= n; ++i) fast = fast!.next;
27+
// if the previous value is null but next is not
28+
ListNode? prev = null;
29+
// while the first pointer is not null
30+
while (fast != null) {
31+
// fast will hold the next value
32+
fast = fast.next;
33+
// previous will hold the second pointer
34+
prev = slow;
35+
slow = slow!.next;
36+
}
37+
// if previous is null
38+
if (prev == null) {
39+
40+
// head will pint to the next value
41+
head = head.next;
42+
} else {
43+
prev.next = slow!.next;
44+
slow.next = null;
45+
}
46+
// else we will simply return head
47+
return head;
48+
}
49+
}
50+
```
51+
52+
## Solution - 2 Three Pointers
53+
54+
Approach :- First increase the fast pointer by n steps to ensure that when the fast pointer points to null , at the same time, the slow pointer points to the location of the node which is to be removed
55+
fast pointer : to move n steps ahead of slow pointer
56+
slow pointer : this will point to the exact node which is to be removed once the fast pointer reaches null
57+
prev pointer: this will point to the node before the slow pointer because this pointer will bypass the node which is to be removed and get connected directly to the node next to slow.
58+
59+
```dart
60+
class Solution {
61+
// Runtime: 509 ms, faster than 50.00% of Dart online submissions for Remove Nth Node From End of List.
62+
// Memory Usage: 142.5 MB, less than 79.41% of Dart online submissions for Remove Nth Node From End of List.
63+
ListNode? removeNthFromEnd(ListNode? head, int n) {
64+
ListNode? fast = head;
65+
ListNode? slow = head;
66+
ListNode? prev = null;
67+
//Loop to take the fast pointer n steps ahead of the slow pointer.
68+
while (n > 0) {
69+
fast = fast?.next;
70+
n--;
71+
}
72+
//If fast pointer points to null, it means that the node has to be removed from the beginning.
73+
if (fast == null) return slow?.next;
74+
75+
while (fast != null) {
76+
fast = fast.next;
77+
prev = slow;
78+
slow = slow?.next;
79+
}
80+
81+
if (prev != null)
82+
prev.next = slow?.next;
83+
else if (prev == null) return null;
84+
return head;
85+
}
86+
}
87+
```
88+
89+
## Solution - 3 Without Using Dummy Node
90+
91+
```dart
92+
class Solution {
93+
// Runtime: 488 ms, faster than 52.94% of Dart online submissions for Remove Nth Node From End of List.
94+
// Memory Usage: 142.4 MB, less than 79.41% of Dart online submissions for Remove Nth Node From End of List.
95+
ListNode? removeNthFromEnd(ListNode? head, int n) {
96+
ListNode? fast = head, slow = head;
97+
for (int i = 1; i <= n; i++) fast = fast?.next;
98+
// for cases where n = length of linked-list i.e. if first node is to be deleted
99+
if (fast == null) {
100+
head = head?.next;
101+
return head;
102+
}
103+
while (fast?.next != null) {
104+
slow = slow?.next;
105+
fast = fast?.next;
106+
}
107+
slow?.next = slow.next?.next;
108+
return head;
109+
}
110+
}
111+
```
112+
113+
## Bonus Solution - Golang
114+
115+
```go
116+
// Runtime: 0 ms, faster than 100.00% of Go online submissions for Remove Nth Node From End of List.
117+
// Memory Usage: 2.3 MB, less than 31.13% of Go online submissions for Remove Nth Node From End of List.
118+
119+
func removeNthFromEnd(head *ListNode, n int) *ListNode {
120+
dummy := new(ListNode)
121+
dummy.Next = head
122+
fast := head
123+
slow := dummy
124+
for i := 0; i < n; i++ {
125+
fast = fast.Next
126+
}
127+
128+
for fast != nil {
129+
fast = fast.Next
130+
slow = slow.Next
131+
}
132+
133+
slow.Next = slow.Next.Next
134+
135+
return dummy.Next
136+
}
137+
138+
```

0 commit comments

Comments
 (0)