20 lines
630 B
Python
20 lines
630 B
Python
#https://leetcode.com/problems/merge-nodes-in-between-zeros
|
|
class ListNode:
|
|
def __init__(self, val=0, next=None):
|
|
self.val = val
|
|
self.next = next
|
|
class Solution:
|
|
def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
|
|
left = ListNode(-1, None)
|
|
left.next = head
|
|
curr = head
|
|
val = 0
|
|
while curr is not None:
|
|
if curr.val == 0 and left.next != curr:
|
|
left.next.val = val
|
|
left = left.next
|
|
val = 0
|
|
val += curr.val
|
|
curr = curr.next
|
|
left.next = None
|
|
return head
|