Skip to content

Latest commit

 

History

History
182 lines (164 loc) · 4.37 KB

_2181. Merge Nodes in Between Zeros.md

File metadata and controls

182 lines (164 loc) · 4.37 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 04, 2024

Last updated : July 04, 2024


Related Topics : Linked List, Simulation

Acceptance Rate : 89.79 %


Since we only want a linkedlist of sums, if we have a non-zero value after we can just add it to our current node and delete it. What I decided to do was add the nodes to the zero node, and once another zero node was reached, we just move the current spot to that next pointer.

For instance:

0 -> 3 -> 1 -> 0 -> 4 -> 5 -> 2 -> 0
3      -> 1 -> 0 -> 4 -> 5 -> 2 -> 0
4           -> 0 -> 4 -> 5 -> 2 -> 0
4                -> 4 -> 5 -> 2 -> 0
4                -> 9      -> 2 -> 0
4                -> 11          -> 0
4                -> 11
4 -> 11

return
0 -> 1 -> 0 -> 3 -> 0 -> 2 -> 2 -> 0
1      -> 0 -> 3 -> 0 -> 2 -> 2 -> 0
1      -> 3      -> 0 -> 2 -> 2 -> 0
1      -> 3      -> 2      -> 2 -> 0
1      -> 3      -> 4           -> 0
1      -> 3      -> 4
1 -> 3 -> 4

return

Solutions

C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeNodes(struct ListNode* head) {
    struct ListNode* curr = head;
    while (curr->next) {
        if (curr->next->val == 0) {
            if (!curr->next->next) {
                curr->next = NULL;
                break;
            }
            curr = curr->next;
        } else {
            curr->val += curr->next->val;
            curr->next = curr->next->next;
        }
    }

    return head;
}

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeNodes(ListNode* head) {
        ListNode* curr = head;
        while (curr->next) {
            if (curr->next->val == 0) {
                if (!curr->next->next) {
                    curr->next = NULL;
                    break;
                }
                curr = curr->next;
            } else {
                curr->val += curr->next->val;
                curr->next = curr->next->next;
            }
        }

        return head;
    }
};

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeNodes(ListNode head) {
        ListNode curr = head;
        while (curr.next != null) {
            if (curr.next.val == 0) {
                if (curr.next.next == null) {
                    curr.next = null;
                    break;
                }
                curr = curr.next;
            } else {
                curr.val += curr.next.val;
                curr.next = curr.next.next;
            }
        }

        return head;
    }
}

Python

# Definition for singly-linked list.
# 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]:
        curr = head
        while curr.next :
            if curr.next.val == 0 :
                if curr.next.next == None :
                    curr.next = None
                    break
                curr = curr.next
            else :
                curr.val += curr.next.val
                curr.next = curr.next.next
        return head