Skip to content

Commit 8ca79b7

Browse files
Merge pull request #3 from paxtonfitzpatrick/main
solution and notes for problem 2181
2 parents a6ab788 + 0bb5403 commit 8ca79b7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Each day (ideally) we'll attempt the daily [leetcode](https://leetcode.com) prob
88
|--------------|-------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------|------------|
99
| July 1, 2024 | [350](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Click here](https://github.com/ContextLab/leetcode-solutions/tree/main/problems/350) | Easy |
1010
| July 2, 2024 | [1509](https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/) | [Click here](https://github.com/ContextLab/leetcode-solutions/tree/main/problems/1509) | Medium |
11+
| July 3, 2024 | [2181](https://leetcode.com/problems/merge-nodes-in-between-zeros/) | [Click here](https://github.com/ContextLab/leetcode-solutions/tree/main/problems/2181) | Medium |
1112

1213
# Join our discussion!
1314

problems/2181/paxton.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [Problem 2181: Merge Nodes in Between Zeros](https://leetcode.com/problems/merge-nodes-in-between-zeros/description)
2+
3+
## Initial thoughts (stream-of-consciousness)
4+
- start off by iterating through the linked list from the head node
5+
- whenever we encounter a node with a `.val` of 0, start incrementing a running total of node values beginning with the next node
6+
- when we reach another 0-value node:
7+
- construct a `ListNode` whose `.val` is the running total (and `.next` is `None`, for now)
8+
- if this is the first `ListNode` we've created, it's the head node we need to return at the end...
9+
- ...otherwise, the last node we made needs to point to it -- so set *that* node's `.next` to this new `ListNode`
10+
- store the newly created `ListNode` so that we can update its `.next` the next time we create a new node (if we do)
11+
- reset the running total and keep going
12+
- the last `ListNode` in the input list will have a `.next` of `None`, so we can iterate over the input list using a `while` loop with that as our stopping condition.
13+
- but since that will break the loop before we can create the last `ListNode` from the current running total, we'll need to create the last one after the loop breaks
14+
15+
## Refining the problem
16+
17+
## Attempted solution(s)
18+
```python
19+
# Definition for singly-linked list.
20+
class ListNode:
21+
def __init__(self, val=0, next=None):
22+
self.val = val
23+
self.next = next
24+
25+
26+
class Solution:
27+
def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
28+
curr_input_node = head.next
29+
curr_sum = 0
30+
while curr_input_node.next is not None:
31+
if curr_input_node.val == 0:
32+
new_merged_node = ListNode(val=curr_sum)
33+
try:
34+
prev_merged_node.next = new_merged_node
35+
except NameError:
36+
new_head_node = new_merged_node
37+
curr_sum = 0
38+
prev_merged_node = new_merged_node
39+
else:
40+
curr_sum += curr_input_node.val
41+
42+
curr_input_node = curr_input_node.next
43+
44+
try:
45+
prev_merged_node.next = ListNode(val=curr_sum)
46+
except NameError:
47+
new_head_node = ListNode(val=curr_sum)
48+
49+
return new_head_node
50+
```
51+
52+
![](https://github.com/paxtonfitzpatrick/leetcode-solutions/assets/26118297/bd8a392e-113c-48de-a3f7-ae5217980630)
53+
54+
That's kinda slow... there's definitely a more efficient way to do this; might revisit this one at some point.

0 commit comments

Comments
 (0)