🔥✅Beat 100%✅|| C/C++/Java/JavaScript/Python || Full explanation✅|| Very easy approach/ Technique✅🔥
Please upvote and give a star if you like the solution...\uD83D\uDE42\n\n# Intuition\nThe problem involves a linked list where some nodes have the value 0, and 
# Please upvote and give a star if you like the solution...\uD83D\uDE42\n\n# Intuition\nThe problem involves a linked list where some nodes have the value 0, and our goal is to merge the nodes between these 0 nodes. Specifically, each segment of nodes between two 0 nodes needs to be summed up, and the first node of each segment should hold this sum. The segments themselves should be merged together, effectively removing all the 0 nodes except for the initial one (which is ignored).\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- **Initial Check:** Start by checking if the list is valid and has more than one node. If the next node of head is null, return 0 since there are no segments to process.\n\n- **Initialization:** Skip the initial 0 node since it\u2019s just a placeholder. Set up two pointers, temp and traverse, where temp will keep track of the current segment\'s starting node, and traverse will iterate through the list.\n\n- **Merging Segments:**\n- - Traverse through the list using the traverse pointer.\n- - If the current node pointed by traverse is not 0, add its value to the node pointed by temp.\n- - If the current node pointed by traverse is 0, set the next node of temp to be the node after traverse (skipping the 0 node). Then move temp to this new start of the next segment and continue.\n- **Termination:** The loop continues until all nodes are processed. The final list will consist of the sum nodes linked together.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- **Time Complexity:** O(n)\nThe solution involves a single pass through the linked list where n is the number of nodes. Each node is visited exactly once.\n- **Space Complexity:** O(1)\nThe solution uses a constant amount of extra space. No additional data structures proportional to the input size are used.\n\n# Code\n```C []\nstruct ListNode* mergeNodes(struct ListNode* head) {\n    // If the list contains only one node (which should be zero according to problem constraints), return 0\n    if (head->next == NULL) {\n        return 0;\n    }\n\n    // Skip the initial zero node\n    head = head->next;\n\n    // Initialize two pointers\n    struct ListNode* temp = head;     // \'temp\' will point to the current node being processed for summing\n    struct ListNode* traverse = temp->next; // \'traverse\' will iterate through the list\n\n    // Traverse the list\n    while (temp->next != NULL) {\n        if (traverse->val != 0) {\n            // If \'traverse\' is not zero, add its value to \'temp\'\n            temp->val += traverse->val;\n            traverse = traverse->next;\n        } else {\n            // If \'traverse\' is zero, it indicates the end of the current segment\n            temp->next = traverse->next; // Skip the zero node\n\n            // If the end of the list is reached after skipping the zero\n            if (temp->next == NULL) {\n                return head;\n            }\n\n            // Move \'temp\' to the start of the next segment\n            temp = traverse->next;\n            traverse = temp->next; // Move \'traverse\' to the next node\n        }\n    }\n\n    return 0; // This line will never be reached if the input is valid according to problem constraints\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        // If the list contains only one node (which should be zero according to problem constraints), return 0\n        if(head->next == nullptr) {\n            return 0;\n        }\n\n        // Skip the initial zero node\n        head = head->next;\n\n        // Initialize two pointers\n        struct ListNode* temp = head;     // \'temp\' will point to the current node being processed for summing\n        struct ListNode* traverse = temp->next; // \'traverse\' will iterate through the list\n\n        // Traverse the list\n        while (temp->next != nullptr) {\n            if (traverse->val != 0) {\n                // If \'traverse\' is not zero, add its value to \'temp\'\n                temp->val += traverse->val;\n                traverse = traverse->next;\n            } else {\n                // If \'traverse\' is zero, it indicates the end of the current segment\n                temp->next = traverse->next; // Skip the zero node\n\n                // If the end of the list is reached after skipping the zero\n                if(temp->next == nullptr) {\n                    return head;\n                }\n\n                // Move \'temp\' to the start of the next segment\n                temp = traverse->next;\n                traverse = temp->next; // Move \'traverse\' to the next node\n            }\n        }\n\n        return 0; // This line will never be reached if the input is valid according to problem constraints\n    }\n};\n\n```\n```Java []\npublic ListNode mergeNodes(ListNode head) {\n    // If the list contains only one node (which should be zero according to problem constraints), return 0\n    if (head.next == null) {\n        return null;\n    }\n\n    // Skip the initial zero node\n    head = head.next;\n\n    // Initialize two pointers\n    ListNode temp = head;     // \'temp\' will point to the current node being processed for summing\n    ListNode traverse = temp.next; // \'traverse\' will iterate through the list\n\n    // Traverse the list\n    while (temp.next != null) {\n        if (traverse.val != 0) {\n            // If \'traverse\' is not zero, add its value to \'temp\'\n            temp.val += traverse.val;\n            traverse = traverse.next;\n        } else {\n            // If \'traverse\' is zero, it indicates the end of the current segment\n            temp.next = traverse.next; // Skip the zero node\n\n            // If the end of the list is reached after skipping the zero\n            if (temp.next == null) {\n                return head;\n            }\n\n            // Move \'temp\' to the start of the next segment\n            temp = traverse.next;\n            traverse = temp.next; // Move \'traverse\' to the next node\n        }\n    }\n\n    return null; // This line will never be reached if the input is valid according to problem constraints\n}\n\n```\n```JavaScript []\nfunction mergeNodes(head) {\n    // If the list contains only one node (which should be zero according to problem constraints), return 0\n    if (head.next === null) {\n        return null;\n    }\n\n    // Skip the initial zero node\n    head = head.next;\n\n    // Initialize two pointers\n    let temp = head;     // \'temp\' will point to the current node being processed for summing\n    let traverse = temp.next; // \'traverse\' will iterate through the list\n\n    // Traverse the list\n    while (temp.next !== null) {\n        if (traverse.val !== 0) {\n            // If \'traverse\' is not zero, add its value to \'temp\'\n            temp.val += traverse.val;\n            traverse = traverse.next;\n        } else {\n            // If \'traverse\' is zero, it indicates the end of the current segment\n            temp.next = traverse.next; // Skip the zero node\n\n            // If the end of the list is reached after skipping the zero\n            if (temp.next === null) {\n                return head;\n            }\n\n            // Move \'temp\' to the start of the next segment\n            temp = traverse.next;\n            traverse = temp.next; // Move \'traverse\' to the next node\n        }\n    }\n\n    return null; // This line will never be reached if the input is valid according to problem constraints\n}\n\n```\n```Python []\nclass ListNode:\n    def __init__(self, val=0, next=None):\n        self.val = val\n        self.next = next\n\ndef mergeNodes(head):\n    # If the list contains only one node (which should be zero according to problem constraints), return 0\n    if head.next is None:\n        return None\n\n    # Skip the initial zero node\n    head = head.next\n\n    # Initialize two pointers\n    temp = head     # \'temp\' will point to the current node being processed for summing\n    traverse = temp.next # \'traverse\' will iterate through the list\n\n    # Traverse the list\n    while temp.next is not None:\n        if traverse.val != 0:\n            # If \'traverse\' is not zero, add its value to \'temp\'\n            temp.val += traverse.val\n            traverse = traverse.next\n        else:\n            # If \'traverse\' is zero, it indicates the end of the current segment\n            temp.next = traverse.next # Skip the zero node\n\n            # If the end of the list is reached after skipping the zero\n            if temp.next is None:\n                return head\n\n            # Move \'temp\' to the start of the next segment\n            temp = traverse.next\n            traverse = temp.next # Move \'traverse\' to the next node\n\n    return None # This line will never be reached if the input is valid according to problem constraints\n\n```\n\n![LeetCode Upvote.png](https://assets.leetcode.com/users/images/1c3df5bc-2cbe-4b0c-8062-9651615abd45_1720076932.8951452.png)\n
['C', 'Python', 'C++', 'Java', 'JavaScript']