Skip to content

Commit b695773

Browse files
committed
leetcode
1 parent cefa79b commit b695773

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
3+
4+
-* Delete Node in a Linked List *-
5+
6+
There is a singly-linked list head and we want to delete a node node in it.
7+
8+
You are given the node to be deleted node. You will not be given access to the first node of head.
9+
10+
All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list.
11+
12+
Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:
13+
14+
The value of the given node should not exist in the linked list.
15+
The number of nodes in the linked list should decrease by one.
16+
All the values before node should be in the same order.
17+
All the values after node should be in the same order.
18+
Custom testing:
19+
20+
For the input, you should provide the entire linked list head and the node to be given node. node should not be the last node of the list and should be an actual node in the list.
21+
We will build the linked list and pass the node to your function.
22+
The output will be the entire list after calling your function.
23+
24+
25+
Example 1:
26+
27+
28+
Input: head = [4,5,1,9], node = 5
29+
Output: [4,1,9]
30+
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
31+
Example 2:
32+
33+
34+
Input: head = [4,5,1,9], node = 1
35+
Output: [4,5,9]
36+
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
37+
38+
39+
Constraints:
40+
41+
The number of the nodes in the given list is in the range [2, 1000].
42+
-1000 <= Node.val <= 1000
43+
The value of each node in the list is unique.
44+
The node to be deleted is in the list and is not a tail node.
45+
46+
*/
47+
48+
// Definition for singly-linked list.
49+
50+
class TreeNode {
51+
int val;
52+
TreeNode? next;
53+
TreeNode([this.val = 0, this.next]);
54+
}
55+
56+
class A {
57+
void deleteNode(TreeNode? node) {
58+
// Step 1: Set value of current node as value of next node
59+
60+
node?.val = node.next!.val;
61+
62+
// Step 2: Delete the next node from list
63+
node?.next = node.next?.next;
64+
}
65+
}
66+
67+
class B {
68+
void deleteNode(TreeNode? node) {
69+
node = node?.next; //just copy
70+
}
71+
}
72+
73+
class C {
74+
void deleteNode(TreeNode? node) {
75+
TreeNode? temp = node?.next; //store delete node in temp
76+
node?.val = node.next!.val; //copy the data
77+
node?.next = node.next!.next; //making link also break link
78+
deleteNode(temp); //delete the data because don't want memory leak
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
// Runtime: 4 ms, faster than 72.67% of Go online submissions for Delete Node in a Linked List.
9+
// Memory Usage: 2.9 MB, less than 67.73% of Go online submissions for Delete Node in a Linked List.
10+
// func deleteNode(node *ListNode) {
11+
12+
// node.Val = node.Next.Val
13+
// node.Next = node.Next.Next
14+
// }
15+
16+
func deleteNode(node *ListNode) {
17+
18+
node.Val = node.Next.Val
19+
node.Next = node.Next.Next
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 🔥 Delete Node in a Linked List 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation TC(1) || Space(1)
2+
3+
## Definition for singly-linked list
4+
5+
```dart
6+
class TreeNode {
7+
int val;
8+
TreeNode? next;
9+
TreeNode([this.val = 0, this.next]);
10+
}
11+
```
12+
13+
## Solution - 1
14+
15+
```dart
16+
class Solution {
17+
void deleteNode(TreeNode? node) {
18+
// Step 1: Set value of current node as value of next node
19+
20+
node?.val = node.next!.val;
21+
22+
// Step 2: Delete the next node from list
23+
node?.next = node.next?.next;
24+
}
25+
}
26+
```
27+
28+
## Solution - 2
29+
30+
```dart
31+
class Solution {
32+
void deleteNode(TreeNode? node) {
33+
node = node?.next; //just copy
34+
}
35+
}
36+
```
37+
38+
## Solution - 3
39+
40+
```dart
41+
class Solution {
42+
void deleteNode(TreeNode? node) {
43+
TreeNode? temp = node?.next; //store delete node in temp
44+
node?.val = node.next!.val; //copy the data
45+
node?.next = node.next!.next; //making link also break link
46+
deleteNode(temp); //delete the data because don't want memory leak
47+
}
48+
}
49+
```
50+
51+
## Disclaimer:-
52+
53+
### This Solution is not available in DART Programing language which is a bummer. Hurts my feeling. But as a man we should implement it no matter what. We are not bunch of wussies who gonna skip it if it's not available in one language we love. Instead we will conquer the sea and rivers and cross the mountains so see what's lies beyond our horizons

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
9292
- [Shortest Word Distance](ShortestWordDistance/shortest_word_distance.dart)
9393
- [Largest Perimeter Triangle](LargestPerimeterTriangle/largest_perimeter_triangle.dart)
9494
- [Binary Tree Paths](BinaryTreePaths/binary_tree_paths.dart)
95+
- [Delete Node in a Linked List](DeleteNodeInALinkedList/delete_node_in_a_linked_list.dart)
9596

9697
## Reach me via
9798

0 commit comments

Comments
 (0)