Skip to content

Commit 008ad30

Browse files
committed
leetcode
1 parent f7a94a5 commit 008ad30

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
6969
- [Reverse Bits](ReverseBits/reverse_bits.dart)
7070
- [Happy Number](HappyNumber/happy_number.dart)
7171
- [Number of Dice Rolls With Target Sum](NumberOfDiceRollsWithTargetSum/number_of_dice_rolls_with_target_sum.dart)
72+
- [Remove Linked List Elements](RemoveLinkedListElements/remove_linked_list_elements.dart)
7273

7374
## Reach me via
7475

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 🔥 Remove Linked List Elements 🔥 || 3 SOlutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 Recursive
4+
5+
```dart
6+
class Solution {
7+
ListNode? removeElements(ListNode? head, int val) {
8+
// if the head iss null return null because it's empty
9+
if (head == null) return null;
10+
// if he next element is than remove the next element and val
11+
head.next = removeElements(head.next, val);
12+
// if the head is same as val we will to the next
13+
if (head.val == val) return head.next;
14+
// and return the head value whatever wwe have left
15+
return head;
16+
}
17+
}
18+
```
19+
20+
## Solution - 2 Two Pointers
21+
22+
```dart
23+
class Solution {
24+
ListNode? removeElements(ListNode? head, int val) {
25+
// if the head is null means there is no element
26+
if (head == null) return null;
27+
// currently we are at the first head which is our current value
28+
ListNode? current = head;
29+
// previous is null because we have not went ahead of the current head
30+
ListNode? previous = null;
31+
// Assuming that the our current head is not null
32+
while (current != null) {
33+
// if at the head the value is same our given value
34+
if (current.val == val) {
35+
// and the while going forward that the previous is also not null
36+
if (previous != null)
37+
// than we will move forward
38+
previous.next = current.next;
39+
else
40+
// Than our head will point to the next value value to reach the next value
41+
head = head?.next;
42+
} else {
43+
// otherwise our previous will stay the current
44+
previous = current;
45+
}
46+
// or current will point to the next current
47+
current = current.next;
48+
}
49+
return head;
50+
}
51+
}
52+
```
53+
54+
## Solution - 3
55+
56+
```dart
57+
class Solution {
58+
ListNode? removeElements(ListNode? head, int val) {
59+
ListNode? helper = ListNode(0);
60+
helper.next = head;
61+
ListNode p = helper;
62+
63+
while (p.next != null) {
64+
if (p.next?.val == val) {
65+
ListNode? next = p.next;
66+
p.next = next?.next;
67+
} else {
68+
p = p.next!;
69+
}
70+
}
71+
72+
return helper.next;
73+
}
74+
}
75+
```
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
3+
-* Remove Linked List Elements *-
4+
5+
6+
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
7+
8+
9+
10+
Example 1:
11+
12+
13+
Input: head = [1,2,6,3,4,5,6], val = 6
14+
Output: [1,2,3,4,5]
15+
Example 2:
16+
17+
Input: head = [], val = 1
18+
Output: []
19+
Example 3:
20+
21+
Input: head = [7,7,7,7], val = 7
22+
Output: []
23+
24+
25+
Constraints:
26+
27+
The number of nodes in the list is in the range [0, 104].
28+
1 <= Node.val <= 50
29+
0 <= val <= 50
30+
31+
*/
32+
33+
// Definition for singly-linked list.
34+
class ListNode {
35+
int val;
36+
ListNode? next;
37+
ListNode([this.val = 0, this.next]);
38+
}
39+
40+
class A {
41+
ListNode? removeElements(ListNode? head, int val) {
42+
ListNode? helper = ListNode(0);
43+
helper.next = head;
44+
ListNode p = helper;
45+
46+
while (p.next != null) {
47+
if (p.next?.val == val) {
48+
ListNode? next = p.next;
49+
p.next = next?.next;
50+
} else {
51+
p = p.next!;
52+
}
53+
}
54+
55+
return helper.next;
56+
}
57+
}
58+
59+
class B {
60+
ListNode? removeElements(ListNode? head, int val) {
61+
// if the head is null means there is no element
62+
if (head == null) return null;
63+
// currently we are at the first head which is our current value
64+
ListNode? current = head;
65+
// previous is null because we have not went ahead of the current head
66+
ListNode? previous = null;
67+
// Assuming that the our current head is not null
68+
while (current != null) {
69+
// if at the head the value is same our given value
70+
if (current.val == val) {
71+
// and the while going forward that the previous is also not null
72+
if (previous != null)
73+
// than we will move forward
74+
previous.next = current.next;
75+
else
76+
// Than our head will point to the next value value to reach the next value
77+
head = head?.next;
78+
} else {
79+
// otherwise our previous will stay the current
80+
previous = current;
81+
}
82+
// or current will point to the next current
83+
current = current.next;
84+
}
85+
return head;
86+
}
87+
}
88+
89+
class C {
90+
ListNode? removeElements(ListNode? head, int val) {
91+
// if the head iss null return null because it's empty
92+
if (head == null) return null;
93+
// if he next element is than remove the next element and val
94+
head.next = removeElements(head.next, val);
95+
// if the head is same as val we will to the next
96+
if (head.val == val) return head.next;
97+
// and return the head value whatever wwe have left
98+
return head;
99+
}
100+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func removeElements(head *ListNode, val int) *ListNode {
10+
if head == nil {
11+
return nil
12+
}
13+
head.Next = removeElements(head.Next, val)
14+
if head.Val == val {
15+
return head.Next
16+
}
17+
return head
18+
}

0 commit comments

Comments
 (0)