🏆💢💯 Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\n\n Describe your first thoughts on how to solve this problem. \nC++ []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *   
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/9fb65eee-6586-4dca-95ee-e8c31b0e42e1_1717038315.9241626.jpeg)\n![Screenshot 2024-05-30 082510.png](https://assets.leetcode.com/users/images/47fd1b3e-8d3b-4c01-8cd3-541a613d33d0_1717038323.829051.png)\n![Screenshot 2024-05-30 081918.png](https://assets.leetcode.com/users/images/7c63c0ac-c244-4fb7-a4b2-4f5c57a69318_1717038329.271753.png)\n![Screenshot 2024-05-30 081900.png](https://assets.leetcode.com/users/images/917c8996-5fff-4b67-abc1-2c1acbfdc76f_1717038335.0618167.png)\n<!-- Describe your first thoughts on how to solve this problem. -->\n```C++ []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        stack<int> stk;\n        int sum = 0;\n        while (head) {\n            sum += head->val;\n            if (head->val == 0 and sum) {\n                stk.push(sum);\n                sum = 0;\n            }\n            head = head->next;\n        }\n        ListNode* prev = NULL;\n        while (!stk.empty()) {\n            head = new ListNode(stk.top(), prev);\n            stk.pop();\n            prev = head;\n        }\n        return head;\n    }\n};\n```\n```python3 []\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:\n        stack = []\n        sum = 0\n        while head:\n            sum += head.val\n            if head.val == 0 and sum:\n                stack.append(sum)\n                sum = 0\n            head = head.next\n        prev = None\n        while stack:\n            head = ListNode(stack.pop())\n            head.next = prev\n            prev = head\n        return head\n```\n```Java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n * int val;\n * ListNode next;\n * ListNode() {}\n * ListNode(int val) { this.val = val; }\n * ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode mergeNodes(ListNode head) {\n        ListNode ptr1 = head;\n        ListNode ptr2 = head.next;\n        int s = 0;\n        while (ptr2 != null) {\n            if (ptr2.val != 0)\n                s += ptr2.val;\n            else {\n                ptr1.next = ptr2;\n                ptr2.val = s;\n                ptr1 = ptr2;\n                s = 0;\n            }\n            ptr2 = ptr2.next;\n        }\n        return head.next;\n    }\n}\n```\n```C []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     struct ListNode *next;\n * };\n */\nstruct ListNode* mergeNodes(struct ListNode* head) {\n    int sum = 0;\n    struct ListNode* temp = head;\n    struct ListNode* move = temp->next;\n    while (move != NULL) {\n        if (move->val != 0) {\n            sum += move->val;\n        } else {\n            temp->next = move;\n            move->val = sum;\n            temp = move;\n            sum = 0;\n        }\n        move = move->next;\n    }\n    return head->next;\n}\n```\n```python []\n# Definition for singly-linked list.\n# class ListNode(object):\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution(object):\n    def mergeNodes(self, head):\n        """\n        :type head: Optional[ListNode]\n        :rtype: Optional[ListNode]\n        """\n        ptr1 = head\n        ptr2 = head.next\n        s = 0\n        \n        while ptr2:\n            if ptr2.val == 0:\n                ptr1 = ptr1.next\n                ptr1.val = s\n                s = 0\n            else:\n                s += ptr2.val\n            ptr2 = ptr2.next\n\n        ptr1.next = None\n        return head.next\n```\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n# Code\n```\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n * int val;\n * ListNode next;\n * ListNode() {}\n * ListNode(int val) { this.val = val; }\n * ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode mergeNodes(ListNode head) {\n        ListNode ptr1 = head;\n        ListNode ptr2 = head.next;\n        int s = 0;\n        while (ptr2 != null) {\n            if (ptr2.val != 0)\n                s += ptr2.val;\n            else {\n                ptr1.next = ptr2;\n                ptr2.val = s;\n                ptr1 = ptr2;\n                s = 0;\n            }\n            ptr2 = ptr2.next;\n        }\n        return head.next;\n    }\n}\n```\n# ***Please upvote if you find my solution useful***\n![th.jpeg](https://assets.leetcode.com/users/images/81bb7a2e-c15a-4571-8c6f-298aa574f861_1717038511.57845.jpeg)\n
['Linked List', 'C', 'Simulation', 'Python', 'C++', 'Java', 'Python3']