Skip to content

Commit bbc7753

Browse files
committed
Daily
1 parent f94d4d8 commit bbc7753

File tree

10 files changed

+305
-0
lines changed

10 files changed

+305
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ Profile: [Zanger](https://leetcode.com/u/Zanger/)
318318
| 2130 | [Maximum Twin Sum of a Linked List](<https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list>) | Medium | | [solution](<markdowns/_2130. Maximum Twin Sum of a Linked List.md>) | py |
319319
| 2149 | [Rearrange Array Elements by Sign](<https://leetcode.com/problems/rearrange-array-elements-by-sign>) | Medium | | [solution](<markdowns/_2149. Rearrange Array Elements by Sign.md>) | py, c |
320320
| 2165 | [Smallest Value of the Rearranged Number](<https://leetcode.com/problems/smallest-value-of-the-rearranged-number>) | Medium | | [solution](<markdowns/_2165. Smallest Value of the Rearranged Number.md>) | py |
321+
| 2181 | [Merge Nodes in Between Zeros](<https://leetcode.com/problems/merge-nodes-in-between-zeros>) | Medium | Daily | [solution](<markdowns/_2181. Merge Nodes in Between Zeros.md>) | java, cpp, c, py |
321322
| 2192 | [All Ancestors of a Node in a Directed Acyclic Graph](<https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph>) | Medium | Daily | [solution](<markdowns/_2192. All Ancestors of a Node in a Directed Acyclic Graph.md>) | java, py |
322323
| 2206 | [Divide Array Into Equal Pairs](<https://leetcode.com/problems/divide-array-into-equal-pairs>) | Easy | | [solution](<markdowns/_2206. Divide Array Into Equal Pairs.md>) | py, c |
323324
| 2221 | [Find Triangular Sum of an Array](<https://leetcode.com/problems/find-triangular-sum-of-an-array>) | Medium | | [solution](<markdowns/_2221. Find Triangular Sum of an Array.md>) | java, c |

markdowns/Daily_Questions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Dates are for the date I completed the question so due to the my time zone and h
44

55
| # | Title | Level | Cats | Solution | Languages | Date Complete |
66
|-----:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:------------|:---------------------------------------------------------------------------------------------------|:-----------------|:----------------|
7+
| 2181 | [Merge Nodes in Between Zeros](<https://leetcode.com/problems/merge-nodes-in-between-zeros>) | Medium | Daily | [solution](<_2181. Merge Nodes in Between Zeros.md>) | java, cpp, c, py | Jul 03, 2024 |
78
| 1509 | [Minimum Difference Between Largest and Smallest Value in Three Moves](<https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves>) | Medium | Daily | [solution](<_1509. Minimum Difference Between Largest and Smallest Value in Three Moves.md>) | cpp, py, c, java | Jul 02, 2024 |
89
| 350 | [Intersection of Two Arrays II](<https://leetcode.com/problems/intersection-of-two-arrays-ii>) | Easy | Daily | [solution](<_350. Intersection of Two Arrays II.md>) | java, py | Jul 01, 2024 |
910
| 1550 | [Three Consecutive Odds](<https://leetcode.com/problems/three-consecutive-odds>) | Easy | Daily | [solution](<_1550. Three Consecutive Odds.md>) | py, c | Jun 30, 2024 |

markdowns/Questions_By_Code_Length.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Calculations are based on the code files's byte sizes.
3434
| 987 | [Vertical Order Traversal of a Binary Tree](<https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree>) | Hard | | [solution](<_987. Vertical Order Traversal of a Binary Tree.md>) | py | Jun 11, 2024 |
3535
| 994 | [Rotting Oranges](<https://leetcode.com/problems/rotting-oranges>) | Medium | N150 | [solution](<_994. Rotting Oranges.md>) | py | Jun 14, 2024 |
3636
| 382 | [Linked List Random Node](<https://leetcode.com/problems/linked-list-random-node>) | Medium | | [solution](<_382. Linked List Random Node.md>) | java, py, c | Jun 21, 2024 |
37+
| 2181 | [Merge Nodes in Between Zeros](<https://leetcode.com/problems/merge-nodes-in-between-zeros>) | Medium | Daily | [solution](<_2181. Merge Nodes in Between Zeros.md>) | java, cpp, c, py | Jul 03, 2024 |
3738
| 739 | [Daily Temperatures](<https://leetcode.com/problems/daily-temperatures>) | Medium | N150 | [solution](<_739. Daily Temperatures.md>) | java, py, c | Jun 13, 2024 |
3839
| 1650 | [Lowest Common Ancestor of a Binary Tree III](<https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iii>) | Medium | | [solution](<_1650. Lowest Common Ancestor of a Binary Tree III.md>) | py | Jun 09, 2024 |
3940
| 2192 | [All Ancestors of a Node in a Directed Acyclic Graph](<https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph>) | Medium | Daily | [solution](<_2192. All Ancestors of a Node in a Directed Acyclic Graph.md>) | java, py | Jun 28, 2024 |

markdowns/Questions_By_Recent.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Calculations are based on the date of the first solve.
44

55
| # | Title | Level | Cats | Solution | Languages | Date Complete |
66
|-----:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:---------------|:---------------------------------------------------------------------------------------------------|:---------------------------------|:----------------|
7+
| 2181 | [Merge Nodes in Between Zeros](<https://leetcode.com/problems/merge-nodes-in-between-zeros>) | Medium | Daily | [solution](<_2181. Merge Nodes in Between Zeros.md>) | java, cpp, c, py | Jul 03, 2024 |
78
| 687 | [Longest Univalue Path](<https://leetcode.com/problems/longest-univalue-path>) | Medium | | [solution](<_687. Longest Univalue Path.md>) | py | Jul 03, 2024 |
89
| 437 | [Path Sum III](<https://leetcode.com/problems/path-sum-iii>) | Medium | | [solution](<_437. Path Sum III.md>) | py | Jul 03, 2024 |
910
| 112 | [Path Sum](<https://leetcode.com/problems/path-sum>) | Easy | | [solution](<_112. Path Sum.md>) | py | Jul 03, 2024 |
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# 2181. [Merge Nodes in Between Zeros](<https://leetcode.com/problems/merge-nodes-in-between-zeros>)
2+
3+
*First completed: July 03, 2024*
4+
5+
*Last updated: July 03, 2024*
6+
7+
8+
> *To see the question prompt, click the title.*
9+
10+
**Topics:** Linked List, Simulation
11+
12+
**AC %:** 85.779
13+
14+
15+
------
16+
Since we only want a linkedlist of sums, if we have a non-zero value after we can just add it to our current node and delete it. What I decided to do was add the nodes to the zero node, and once another zero node was reached, we just move the current spot to that next pointer.
17+
18+
For instance:
19+
```
20+
0 -> 3 -> 1 -> 0 -> 4 -> 5 -> 2 -> 0
21+
3 -> 1 -> 0 -> 4 -> 5 -> 2 -> 0
22+
4 -> 0 -> 4 -> 5 -> 2 -> 0
23+
4 -> 4 -> 5 -> 2 -> 0
24+
4 -> 9 -> 2 -> 0
25+
4 -> 11 -> 0
26+
4 -> 11
27+
4 -> 11
28+
29+
return
30+
```
31+
32+
```
33+
0 -> 1 -> 0 -> 3 -> 0 -> 2 -> 2 -> 0
34+
1 -> 0 -> 3 -> 0 -> 2 -> 2 -> 0
35+
1 -> 3 -> 0 -> 2 -> 2 -> 0
36+
1 -> 3 -> 2 -> 2 -> 0
37+
1 -> 3 -> 4 -> 0
38+
1 -> 3 -> 4
39+
1 -> 3 -> 4
40+
41+
return
42+
```
43+
44+
45+
46+
## Solutions
47+
48+
- [m2181 Daily.c](<../my-submissions/m2181 Daily.c>)
49+
- [m2181.cpp](<../my-submissions/m2181.cpp>)
50+
- [m2181.java](<../my-submissions/m2181.java>)
51+
- [m2181.py](<../my-submissions/m2181.py>)
52+
### C
53+
#### [m2181 Daily.c](<../my-submissions/m2181 Daily.c>)
54+
```C
55+
/**
56+
* Definition for singly-linked list.
57+
* struct ListNode {
58+
* int val;
59+
* struct ListNode *next;
60+
* };
61+
*/
62+
struct ListNode* mergeNodes(struct ListNode* head) {
63+
struct ListNode* curr = head;
64+
while (curr->next) {
65+
if (curr->next->val == 0) {
66+
if (!curr->next->next) {
67+
curr->next = NULL;
68+
break;
69+
}
70+
curr = curr->next;
71+
} else {
72+
curr->val += curr->next->val;
73+
curr->next = curr->next->next;
74+
}
75+
}
76+
77+
return head;
78+
}
79+
```
80+
81+
### C++
82+
#### [m2181.cpp](<../my-submissions/m2181.cpp>)
83+
```C++
84+
/**
85+
* Definition for singly-linked list.
86+
* struct ListNode {
87+
* int val;
88+
* ListNode *next;
89+
* ListNode() : val(0), next(nullptr) {}
90+
* ListNode(int x) : val(x), next(nullptr) {}
91+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
92+
* };
93+
*/
94+
class Solution {
95+
public:
96+
ListNode* mergeNodes(ListNode* head) {
97+
ListNode* curr = head;
98+
while (curr->next) {
99+
if (curr->next->val == 0) {
100+
if (!curr->next->next) {
101+
curr->next = NULL;
102+
break;
103+
}
104+
curr = curr->next;
105+
} else {
106+
curr->val += curr->next->val;
107+
curr->next = curr->next->next;
108+
}
109+
}
110+
111+
return head;
112+
}
113+
};
114+
```
115+
116+
### Java
117+
#### [m2181.java](<../my-submissions/m2181.java>)
118+
```Java
119+
/**
120+
* Definition for singly-linked list.
121+
* public class ListNode {
122+
* int val;
123+
* ListNode next;
124+
* ListNode() {}
125+
* ListNode(int val) { this.val = val; }
126+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
127+
* }
128+
*/
129+
class Solution {
130+
public ListNode mergeNodes(ListNode head) {
131+
ListNode curr = head;
132+
while (curr.next != null) {
133+
if (curr.next.val == 0) {
134+
if (curr.next.next == null) {
135+
curr.next = null;
136+
break;
137+
}
138+
curr = curr.next;
139+
} else {
140+
curr.val += curr.next.val;
141+
curr.next = curr.next.next;
142+
}
143+
}
144+
145+
return head;
146+
}
147+
}
148+
```
149+
150+
### Python
151+
#### [m2181.py](<../my-submissions/m2181.py>)
152+
```Python
153+
# Definition for singly-linked list.
154+
# class ListNode:
155+
# def __init__(self, val=0, next=None):
156+
# self.val = val
157+
# self.next = next
158+
class Solution:
159+
def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
160+
curr = head
161+
while curr.next :
162+
if curr.next.val == 0 :
163+
if curr.next.next == None :
164+
curr.next = None
165+
break
166+
curr = curr.next
167+
else :
168+
curr.val += curr.next.val
169+
curr.next = curr.next.next
170+
return head
171+
```
172+

my-submissions/m2181 Daily.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
struct ListNode* mergeNodes(struct ListNode* head) {
9+
struct ListNode* curr = head;
10+
while (curr->next) {
11+
if (curr->next->val == 0) {
12+
if (!curr->next->next) {
13+
curr->next = NULL;
14+
break;
15+
}
16+
curr = curr->next;
17+
} else {
18+
curr->val += curr->next->val;
19+
curr->next = curr->next->next;
20+
}
21+
}
22+
23+
return head;
24+
}

my-submissions/m2181.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeNodes(ListNode* head) {
14+
ListNode* curr = head;
15+
while (curr->next) {
16+
if (curr->next->val == 0) {
17+
if (!curr->next->next) {
18+
curr->next = NULL;
19+
break;
20+
}
21+
curr = curr->next;
22+
} else {
23+
curr->val += curr->next->val;
24+
curr->next = curr->next->next;
25+
}
26+
}
27+
28+
return head;
29+
}
30+
};

my-submissions/m2181.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeNodes(ListNode head) {
13+
ListNode curr = head;
14+
while (curr.next != null) {
15+
if (curr.next.val == 0) {
16+
if (curr.next.next == null) {
17+
curr.next = null;
18+
break;
19+
}
20+
curr = curr.next;
21+
} else {
22+
curr.val += curr.next.val;
23+
curr.next = curr.next.next;
24+
}
25+
}
26+
27+
return head;
28+
}
29+
}

my-submissions/m2181.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Since we only want a linkedlist of sums, if we have a non-zero value after we can just add it to our current node and delete it. What I decided to do was add the nodes to the zero node, and once another zero node was reached, we just move the current spot to that next pointer.
2+
3+
For instance:
4+
```
5+
0 -> 3 -> 1 -> 0 -> 4 -> 5 -> 2 -> 0
6+
3 -> 1 -> 0 -> 4 -> 5 -> 2 -> 0
7+
4 -> 0 -> 4 -> 5 -> 2 -> 0
8+
4 -> 4 -> 5 -> 2 -> 0
9+
4 -> 9 -> 2 -> 0
10+
4 -> 11 -> 0
11+
4 -> 11
12+
4 -> 11
13+
14+
return
15+
```
16+
17+
```
18+
0 -> 1 -> 0 -> 3 -> 0 -> 2 -> 2 -> 0
19+
1 -> 0 -> 3 -> 0 -> 2 -> 2 -> 0
20+
1 -> 3 -> 0 -> 2 -> 2 -> 0
21+
1 -> 3 -> 2 -> 2 -> 0
22+
1 -> 3 -> 4 -> 0
23+
1 -> 3 -> 4
24+
1 -> 3 -> 4
25+
26+
return
27+
```
28+

my-submissions/m2181.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
8+
curr = head
9+
while curr.next :
10+
if curr.next.val == 0 :
11+
if curr.next.next == None :
12+
curr.next = None
13+
break
14+
curr = curr.next
15+
else :
16+
curr.val += curr.next.val
17+
curr.next = curr.next.next
18+
return head

0 commit comments

Comments
 (0)