Skip to content

Commit 98e541e

Browse files
authored
Merge pull request #1041 from nishant0708/203and205
[Feature Request]: Leetcode:203 & 205 #986
2 parents 517cbc3 + f258e1c commit 98e541e

File tree

2 files changed

+350
-0
lines changed

2 files changed

+350
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
id: remove-linked-list-elements
3+
title: Remove Linked List Elements
4+
sidebar_label: 203-Remove-Linked-List-Elements
5+
tags:
6+
- Linked List
7+
- Java
8+
- C++
9+
- Python
10+
- Data Structures
11+
description: "This document provides solutions for removing all nodes from a linked list that have a specific value."
12+
---
13+
14+
## Problem
15+
16+
Given the head of a linked list and an integer `val`, remove all the nodes of the linked list that have `Node.val == val`, and return the new head.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
**Input:** head = [1,2,6,3,4,5,6], val = 6
23+
24+
**Output:** [1,2,3,4,5]
25+
26+
**Example 2:**
27+
28+
**Input:** head = [], val = 1
29+
30+
**Output:** []
31+
32+
**Example 3:**
33+
34+
**Input:** head = [7,7,7,7], val = 7
35+
36+
**Output:** []
37+
38+
### Constraints
39+
40+
- The number of nodes in the list is in the range `[0, 10^4]`.
41+
- `1 <= Node.val <= 50`
42+
- `0 <= val <= 50`
43+
44+
---
45+
46+
## Approach
47+
48+
The approach involves iterating through the linked list and removing nodes that have the specified value. This can be achieved using a dummy node to handle edge cases where the head itself needs to be removed.
49+
50+
### Steps:
51+
52+
1. **Dummy Node:**
53+
- Create a dummy node that points to the head of the list. This helps to easily handle the case where the head node itself needs to be removed.
54+
55+
2. **Iterate and Remove:**
56+
- Use a pointer to iterate through the list, checking each node's value.
57+
- If a node's value equals `val`, adjust the pointers to bypass this node.
58+
59+
3. **Return New Head:**
60+
- Return the next node of the dummy node as the new head of the list.
61+
62+
## Solution for Remove Linked List Elements
63+
64+
### Java Solution
65+
66+
```java
67+
class ListNode {
68+
int val;
69+
ListNode next;
70+
ListNode() {}
71+
ListNode(int val) { this.val = val; }
72+
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
73+
}
74+
75+
class Solution {
76+
public ListNode removeElements(ListNode head, int val) {
77+
// Create a dummy node to handle edge cases
78+
ListNode dummy = new ListNode(0);
79+
dummy.next = head;
80+
81+
// Use a pointer to iterate through the list
82+
ListNode current = dummy;
83+
84+
while (current.next != null) {
85+
if (current.next.val == val) {
86+
// Bypass the node with the value equal to val
87+
current.next = current.next.next;
88+
} else {
89+
// Move to the next node
90+
current = current.next;
91+
}
92+
}
93+
94+
// Return the new head
95+
return dummy.next;
96+
}
97+
}
98+
```
99+
### C++ solution
100+
101+
```cpp
102+
struct ListNode {
103+
int val;
104+
ListNode *next;
105+
ListNode() : val(0), next(nullptr) {}
106+
ListNode(int x) : val(x), next(nullptr) {}
107+
ListNode(int x, ListNode *next) : val(x), next(next) {}
108+
};
109+
110+
class Solution {
111+
public:
112+
ListNode* removeElements(ListNode* head, int val) {
113+
// Create a dummy node to handle edge cases
114+
ListNode* dummy = new ListNode(0);
115+
dummy->next = head;
116+
117+
// Use a pointer to iterate through the list
118+
ListNode* current = dummy;
119+
120+
while (current->next != nullptr) {
121+
if (current->next->val == val) {
122+
// Bypass the node with the value equal to val
123+
current->next = current->next->next;
124+
} else {
125+
// Move to the next node
126+
current = current->next;
127+
}
128+
}
129+
130+
// Return the new head
131+
return dummy->next;
132+
}
133+
};
134+
```
135+
### Python Solution
136+
137+
```python
138+
class ListNode:
139+
def __init__(self, val=0, next=None):
140+
self.val = val
141+
self.next = next
142+
143+
class Solution:
144+
def removeElements(self, head: ListNode, val: int) -> ListNode:
145+
# Create a dummy node to handle edge cases
146+
dummy = ListNode(0)
147+
dummy.next = head
148+
149+
# Use a pointer to iterate through the list
150+
current = dummy
151+
152+
while current.next is not None:
153+
if current.next.val == val:
154+
# Bypass the node with the value equal to val
155+
current.next = current.next.next
156+
else:
157+
# Move to the next node
158+
current = current.next
159+
160+
# Return the new head
161+
return dummy.next
162+
```
163+
### Complexity Analysis
164+
**Time Complexity:** O(n)
165+
166+
>Reason: The algorithm involves a single pass through the linked list, resulting in a time complexity of $O(n)$, where $n$ is the number of nodes in the list.
167+
168+
**Space Complexity:** O(1)
169+
>Reason: The space complexity is $O(1)$ because the algorithm uses a constant amount of extra space.
170+
171+
### References
172+
**LeetCode Problem:** Remove Linked List Elements
173+
174+
**Solution Link:** Remove Linked List Elements Solution on LeetCode
175+
176+
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
id: isomorphic-strings
3+
title: Isomorphic Strings
4+
sidebar_label: 205-Isomorphic-Strings
5+
tags:
6+
- Hash Table
7+
- String
8+
- Java
9+
- C++
10+
- Python
11+
description: "This document provides solutions for determining if two strings are isomorphic."
12+
---
13+
14+
## Problem
15+
16+
Given two strings `s` and `t`, determine if they are isomorphic.
17+
18+
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
19+
20+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
21+
22+
### Examples
23+
24+
**Example 1:**
25+
26+
**Input:** s = "egg", t = "add"
27+
28+
**Output:** true
29+
30+
**Example 2:**
31+
32+
**Input:** s = "foo", t = "bar"
33+
34+
**Output:** false
35+
36+
**Example 3:**
37+
38+
**Input:** s = "paper", t = "title"
39+
40+
**Output:** true
41+
42+
### Constraints
43+
44+
- `1 <= s.length <= 5 * 10^4`
45+
- `t.length == s.length`
46+
- `s` and `t` consist of any valid ASCII characters.
47+
48+
---
49+
50+
## Approach
51+
52+
The problem can be solved by using two hash maps to track the mappings from characters in `s` to `t` and vice versa. If the mappings are consistent throughout the strings, then the strings are isomorphic.
53+
54+
### Steps:
55+
56+
1. **Initialization:**
57+
- Create two hash maps to store the character mappings from `s` to `t` and from `t` to `s`.
58+
59+
2. **Iterate and Map:**
60+
- Iterate through the characters of both strings simultaneously.
61+
- For each character pair, check if the current mapping exists and is consistent with the previous mappings.
62+
- If any inconsistency is found, return false.
63+
64+
3. **Return Result:**
65+
- If the iteration completes without finding any inconsistency, return true.
66+
67+
## Solution for Isomorphic Strings
68+
69+
### Java Solution
70+
71+
```java
72+
import java.util.HashMap;
73+
import java.util.Map;
74+
75+
class Solution {
76+
public boolean isIsomorphic(String s, String t) {
77+
if (s.length() != t.length()) return false;
78+
79+
Map<Character, Character> mapS = new HashMap<>();
80+
Map<Character, Character> mapT = new HashMap<>();
81+
82+
for (int i = 0; i < s.length(); i++) {
83+
char c1 = s.charAt(i);
84+
char c2 = t.charAt(i);
85+
86+
if (mapS.containsKey(c1)) {
87+
if (mapS.get(c1) != c2) return false;
88+
} else {
89+
mapS.put(c1, c2);
90+
}
91+
92+
if (mapT.containsKey(c2)) {
93+
if (mapT.get(c2) != c1) return false;
94+
} else {
95+
mapT.put(c2, c1);
96+
}
97+
}
98+
99+
return true;
100+
}
101+
}
102+
```
103+
### C++ Solution
104+
105+
```cpp
106+
#include <unordered_map>
107+
#include <string>
108+
109+
class Solution {
110+
public:
111+
bool isIsomorphic(std::string s, std::string t) {
112+
if (s.length() != t.length()) return false;
113+
114+
std::unordered_map<char, char> mapS;
115+
std::unordered_map<char, char> mapT;
116+
117+
for (int i = 0; i < s.length(); i++) {
118+
char c1 = s[i];
119+
char c2 = t[i];
120+
121+
if (mapS.count(c1)) {
122+
if (mapS[c1] != c2) return false;
123+
} else {
124+
mapS[c1] = c2;
125+
}
126+
127+
if (mapT.count(c2)) {
128+
if (mapT[c2] != c1) return false;
129+
} else {
130+
mapT[c2] = c1;
131+
}
132+
}
133+
134+
return true;
135+
}
136+
};
137+
```
138+
### Python Solution
139+
140+
```python
141+
class Solution:
142+
def isIsomorphic(self, s: str, t: str) -> bool:
143+
if len(s) != len(t):
144+
return False
145+
146+
mapS = {}
147+
mapT = {}
148+
149+
for c1, c2 in zip(s, t):
150+
if c1 in mapS:
151+
if mapS[c1] != c2:
152+
return False
153+
else:
154+
mapS[c1] = c2
155+
156+
if c2 in mapT:
157+
if mapT[c2] != c1:
158+
return False
159+
else:
160+
mapT[c2] = c1
161+
162+
return True
163+
```
164+
### Complexity Analysis
165+
**Time Complexity:** O(n)
166+
> Reason: The algorithm involves a single pass through both strings, resulting in a time complexity of $O(n)$, where $n$ is the length of the strings.
167+
168+
**Space Complexity:** O(n)
169+
>Reason: The space complexity is $O(n)$ due to the additional hash maps used to store the character mappings.
170+
171+
### References
172+
**LeetCode Problem:** Isomorphic Strings
173+
174+
**Solution Link:** Isomorphic Strings Solution on LeetCode

0 commit comments

Comments
 (0)