2181.cpp (592B)
1 class Solution { 2 public: 3 ListNode *mergeNodes(ListNode *head) { 4 if (!head) return nullptr; 5 for (ListNode *p = head; p->next->next;) 6 if (p->val == 0 && p->next->val != 0 && p->next->next->val != 0) { 7 p->next->next->val += p->next->val; 8 p->next = p->next->next; 9 } else 10 p = p->next; 11 12 for (ListNode *p = head; p->next;) 13 if (!p->next->val) 14 p->next = p->next->next; 15 else 16 p = p->next; 17 18 return head->val ? head : head->next; 19 } 20 };