Skip to content

Commit ea9f325

Browse files
committed
leetcode
1 parent cfa8f15 commit ea9f325

File tree

4 files changed

+289
-0
lines changed

4 files changed

+289
-0
lines changed
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
3+
4+
-* 328. Odd Even Linked List *-
5+
6+
Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
7+
8+
The first node is considered odd, and the second node is even, and so on.
9+
10+
Note that the relative order inside both the even and odd groups should remain as it was in the input.
11+
12+
You must solve the problem in O(1) extra space complexity and O(n) time complexity.
13+
14+
15+
16+
Example 1:
17+
18+
19+
Input: head = [1,2,3,4,5]
20+
Output: [1,3,5,2,4]
21+
Example 2:
22+
23+
24+
Input: head = [2,1,3,5,6,4,7]
25+
Output: [2,3,6,7,1,5,4]
26+
27+
28+
Constraints:
29+
30+
The number of nodes in the linked list is in the range [0, 104].
31+
-106 <= Node.val <= 106
32+
33+
34+
*/
35+
36+
// Definition for singly-linked list.
37+
class ListNode {
38+
int val;
39+
ListNode? next;
40+
ListNode([this.val = 0, this.next]);
41+
}
42+
43+
class A {
44+
ListNode? oddEvenList(ListNode? head) {
45+
if (head != null) {
46+
ListNode? odd = head, even = head.next, evenHead = even;
47+
48+
while (even != null && even.next != null) {
49+
odd?.next = odd.next?.next;
50+
even.next = even.next?.next;
51+
odd = odd?.next;
52+
even = even.next;
53+
}
54+
odd?.next = evenHead;
55+
}
56+
return head;
57+
}
58+
}
59+
60+
class B {
61+
ListNode? oddEvenList(ListNode? head) {
62+
if (head == null || head.next == null) {
63+
return head;
64+
}
65+
66+
ListNode? evenList = ListNode(-1);
67+
ListNode? oddList = ListNode(-1);
68+
69+
ListNode? currentEven = evenList;
70+
ListNode? currentOdd = oddList;
71+
72+
ListNode? current = head;
73+
int i = 1;
74+
while (current != null) {
75+
if (i % 2 != 0) {
76+
currentOdd?.next = current;
77+
currentOdd = current;
78+
current = current.next;
79+
} else {
80+
currentEven?.next = current;
81+
currentEven = current;
82+
current = current.next;
83+
}
84+
i++;
85+
}
86+
currentOdd?.next = evenList.next;
87+
currentEven?.next = null;
88+
return oddList.next;
89+
}
90+
}
91+
92+
class C {
93+
ListNode? oddEvenList(ListNode? head) {
94+
ListNode? lastInEven = null;
95+
ListNode? temp = head;
96+
while (head != null && head.next != null) {
97+
ListNode? next = head.next;
98+
if (lastInEven == null) lastInEven = next;
99+
ListNode? temp = lastInEven?.next;
100+
if (temp == null) break;
101+
head.next = temp;
102+
lastInEven?.next = temp.next;
103+
temp.next = next;
104+
lastInEven = lastInEven?.next;
105+
head = head.next;
106+
if (lastInEven == null) break;
107+
}
108+
return temp;
109+
}
110+
}
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func oddEvenList(head *ListNode) *ListNode {
10+
if head == nil || head.Next == nil {
11+
return head
12+
}
13+
var evenList *ListNode = &ListNode{Val: -1}
14+
var oddList *ListNode = &ListNode{Val: -1}
15+
var currentEven *ListNode = evenList
16+
var currentOdd *ListNode = oddList
17+
var current *ListNode = head
18+
var i int = 1
19+
for current != nil {
20+
if i%2 != 0 {
21+
currentOdd.Next = current
22+
currentOdd = current
23+
current = current.Next
24+
} else {
25+
currentEven.Next = current
26+
currentEven = current
27+
current = current.Next
28+
}
29+
i++
30+
}
31+
currentOdd.Next = evenList.Next
32+
currentEven.Next = nil
33+
return oddList.Next
34+
}
35+
36+
/*
37+
38+
39+
ListNode? oddEvenList(ListNode? head) {
40+
if (head == null || head.next == null) {
41+
return head;
42+
}
43+
44+
ListNode? evenList = ListNode(-1);
45+
ListNode? oddList = ListNode(-1);
46+
47+
ListNode? currentEven = evenList;
48+
ListNode? currentOdd = oddList;
49+
50+
ListNode? current = head;
51+
int i = 1;
52+
while (current != null) {
53+
if (i % 2 != 0) {
54+
currentOdd?.next = current;
55+
currentOdd = current;
56+
current = current.next;
57+
} else {
58+
currentEven?.next = current;
59+
currentEven = current;
60+
current = current.next;
61+
}
62+
i++;
63+
}
64+
currentOdd?.next = evenList.next;
65+
currentEven?.next = null;
66+
return oddList.next;
67+
}
68+
69+
*/
70+
71+
// func oddEvenList(head *ListNode) *ListNode {
72+
// if head == nil {
73+
// return head
74+
// }
75+
// oddNode := head
76+
// evenNode := head.Next
77+
// even := evenNode
78+
// for evenNode != nil && evenNode.Next != nil {
79+
// oddNode.Next = oddNode.Next.Next
80+
// evenNode.Next = evenNode.Next.Next
81+
// oddNode = oddNode.Next
82+
// evenNode = evenNode.Next
83+
// }
84+
// oddNode.Next = even
85+
// return head
86+
// }
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# 🔥 Odd Even 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? oddEvenList(ListNode? head) {
18+
if (head != null) {
19+
ListNode? odd = head, even = head.next, evenHead = even;
20+
21+
while (even != null && even.next != null) {
22+
odd?.next = odd.next?.next;
23+
even.next = even.next?.next;
24+
odd = odd?.next;
25+
even = even.next;
26+
}
27+
odd?.next = evenHead;
28+
}
29+
return head;
30+
}
31+
}
32+
```
33+
34+
## Solution - 2
35+
36+
```dart
37+
class Solution {
38+
ListNode? oddEvenList(ListNode? head) {
39+
if (head == null || head.next == null) {
40+
return head;
41+
}
42+
43+
ListNode? evenList = ListNode(-1);
44+
ListNode? oddList = ListNode(-1);
45+
46+
ListNode? currentEven = evenList;
47+
ListNode? currentOdd = oddList;
48+
49+
ListNode? current = head;
50+
int i = 1;
51+
while (current != null) {
52+
if (i % 2 != 0) {
53+
currentOdd?.next = current;
54+
currentOdd = current;
55+
current = current.next;
56+
} else {
57+
currentEven?.next = current;
58+
currentEven = current;
59+
current = current.next;
60+
}
61+
i++;
62+
}
63+
currentOdd?.next = evenList.next;
64+
currentEven?.next = null;
65+
return oddList.next;
66+
}
67+
}
68+
```
69+
70+
## Solution - 3
71+
72+
```dart
73+
class Solution {
74+
ListNode? oddEvenList(ListNode? head) {
75+
ListNode? lastInEven = null;
76+
ListNode? temp = head;
77+
while (head != null && head.next != null) {
78+
ListNode? next = head.next;
79+
if (lastInEven == null) lastInEven = next;
80+
ListNode? temp = lastInEven?.next;
81+
if (temp == null) break;
82+
head.next = temp;
83+
lastInEven?.next = temp.next;
84+
temp.next = next;
85+
lastInEven = lastInEven?.next;
86+
head = head.next;
87+
if (lastInEven == null) break;
88+
}
89+
return temp;
90+
}
91+
}
92+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
159159
- [**451.** Sort Characters By Frequency](SortCharactersByFrequency/sort_characters_by_frequency.dart)
160160
- [**2256.** Minimum Average Difference](MinimumAverageDifference/minimum_average_difference.dart)
161161
- [**876.** Middle of the Linked List](MiddleOfTheLinkedList/middle_of_the_linked_list.dart)
162+
- [**328.** Odd Even Linked List](OddEvenLinkedList/odd_even_linked_list.dart)
162163

163164
## Reach me via
164165

0 commit comments

Comments
 (0)