Skip to content

Commit cfa8f15

Browse files
committed
leetcode
1 parent d63b9fe commit cfa8f15

File tree

4 files changed

+228
-0
lines changed

4 files changed

+228
-0
lines changed

Diff for: MiddleOfTheLinkedList/middle_of_the_linked_list.dart

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
3+
4+
5+
-* 876. Middle of the Linked List *-
6+
7+
8+
Given the head of a singly linked list, return the middle node of the linked list.
9+
10+
If there are two middle nodes, return the second middle node.
11+
12+
13+
14+
Example 1:
15+
16+
17+
Input: head = [1,2,3,4,5]
18+
Output: [3,4,5]
19+
Explanation: The middle node of the list is node 3.
20+
Example 2:
21+
22+
23+
Input: head = [1,2,3,4,5,6]
24+
Output: [4,5,6]
25+
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
26+
27+
28+
Constraints:
29+
30+
The number of nodes in the list is in the range [1, 100].
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+
class A {
42+
ListNode? middleNode(ListNode? head) {
43+
int n = 0;
44+
ListNode? temp = head;
45+
46+
while (temp!.next != null) {
47+
temp = temp.next;
48+
n++;
49+
}
50+
51+
int mid = (n % 2 == 0) ? (n ~/ 2) : (n ~/ 2 + 1);
52+
53+
ListNode? node = head;
54+
int i = 0;
55+
while (i < mid) {
56+
node = node?.next;
57+
i++;
58+
}
59+
60+
return node;
61+
}
62+
}
63+
64+
class B {
65+
ListNode? middleNode(ListNode? head) {
66+
ListNode? slow = head;
67+
ListNode? fast = head;
68+
69+
while (fast != null && fast.next != null) {
70+
slow = slow!.next;
71+
fast = fast.next!.next;
72+
}
73+
74+
return slow;
75+
}
76+
}
77+
78+
class C {
79+
ListNode? middleNode(ListNode? head) {
80+
int n = 0;
81+
ListNode? current = head;
82+
while (current != null) {
83+
n++;
84+
current = current.next;
85+
}
86+
current = head;
87+
for (int i = 0; i < n / 2; i++) {
88+
current = current?.next;
89+
}
90+
return current;
91+
}
92+
}

Diff for: MiddleOfTheLinkedList/middle_of_the_linked_list.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func middleNode(head *ListNode) *ListNode {
10+
var N int = 0
11+
var Temp *ListNode = head
12+
13+
for Temp.Next != nil {
14+
Temp = Temp.Next
15+
N++
16+
}
17+
var mid int
18+
if N%2 == 0 {
19+
mid = N / 2
20+
} else {
21+
mid = N/2 + 1
22+
}
23+
var node *ListNode = head
24+
var i int = 0
25+
for i < mid {
26+
node = node.Next
27+
i++
28+
}
29+
return node
30+
}
31+
32+
// ===== 2
33+
34+
// func middleNode(head *ListNode) *ListNode {
35+
// var slow *ListNode = head
36+
// var fast *ListNode = head
37+
// for fast != nil && fast.Next != nil {
38+
// slow = slow.Next
39+
// fast = fast.Next.Next
40+
// }
41+
// return slow
42+
// }
43+
44+
// ========== 3
45+
46+
// func middleNode(head *ListNode) *ListNode {
47+
// var n int = 0
48+
// var current *ListNode = head
49+
// for current != nil {
50+
// n++
51+
// current = current.Next
52+
// }
53+
// current = head
54+
// for i := 0; i < n/2; i++ {
55+
// current = current.Next
56+
// }
57+
// return current
58+
// }

Diff for: MiddleOfTheLinkedList/middle_of_the_linked_list.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 🔥 Middle of the Linked List 🔥 || 3 Approaches || 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
14+
15+
```dart
16+
class Solution {
17+
ListNode? middleNode(ListNode? head) {
18+
int n = 0;
19+
ListNode? temp = head;
20+
21+
while (temp!.next != null) {
22+
temp = temp.next;
23+
n++;
24+
}
25+
26+
int mid = (n % 2 == 0) ? (n ~/ 2) : (n ~/ 2 + 1);
27+
28+
ListNode? node = head;
29+
int i = 0;
30+
while (i < mid) {
31+
node = node?.next;
32+
i++;
33+
}
34+
35+
return node;
36+
}
37+
}
38+
```
39+
40+
## Solution - 2 Two Pointers
41+
42+
```dart
43+
class Solution {
44+
ListNode? middleNode(ListNode? head) {
45+
ListNode? slow = head;
46+
ListNode? fast = head;
47+
48+
while (fast != null && fast.next != null) {
49+
slow = slow!.next;
50+
fast = fast.next!.next;
51+
}
52+
53+
return slow;
54+
}
55+
}
56+
```
57+
58+
## Solution - 3 Brute Force
59+
60+
```dart
61+
62+
class Solution {
63+
ListNode? middleNode(ListNode? head) {
64+
int n = 0;
65+
ListNode? current = head;
66+
while (current != null) {
67+
n++;
68+
current = current.next;
69+
}
70+
current = head;
71+
for (int i = 0; i < n / 2; i++) {
72+
current = current?.next;
73+
}
74+
return current;
75+
}
76+
}
77+
```

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
158158
- [**401.** Binary Watch](BinaryWatch/binary_watch.dart)
159159
- [**451.** Sort Characters By Frequency](SortCharactersByFrequency/sort_characters_by_frequency.dart)
160160
- [**2256.** Minimum Average Difference](MinimumAverageDifference/minimum_average_difference.dart)
161+
- [**876.** Middle of the Linked List](MiddleOfTheLinkedList/middle_of_the_linked_list.dart)
161162

162163
## Reach me via
163164

0 commit comments

Comments
 (0)