Skip to content

Commit 30d7435

Browse files
committed
Time: 972 ms (45.39%), Space: 74.9 MB (47.36%) - LeetHub
1 parent 76ae38d commit 30d7435

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
dummy = ListNode(0)
9+
tail = dummy
10+
current = head.next # Skip the initial zero
11+
sum_val = 0
12+
13+
while current:
14+
if current.val == 0:
15+
if sum_val > 0:
16+
tail.next = ListNode(sum_val)
17+
tail = tail.next
18+
sum_val = 0
19+
else:
20+
sum_val += current.val
21+
current = current.next
22+
23+
return dummy.next

0 commit comments

Comments
 (0)