diff options
author | Jack Sangdahl <[email protected]> | 2024-07-03 21:18:14 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-07-03 21:18:14 -0600 |
commit | 1d8e4b171520eba615ef98d63a01e94620142057 (patch) | |
tree | 6c73dbff644caf6be120065e94864abc3e0ffa61 | |
parent | d9929fb2bd46e35c3372a26194e869b0cb078bf4 (diff) | |
download | leetcode-1d8e4b171520eba615ef98d63a01e94620142057.tar.gz leetcode-1d8e4b171520eba615ef98d63a01e94620142057.zip |
2181. merge nodes in between zeros
-rw-r--r-- | merge_nodes_between_zero.c | 59 | ||||
-rw-r--r-- | merge_nodes_between_zero.py | 37 |
2 files changed, 96 insertions, 0 deletions
diff --git a/merge_nodes_between_zero.c b/merge_nodes_between_zero.c new file mode 100644 index 0000000..e51869f --- /dev/null +++ b/merge_nodes_between_zero.c @@ -0,0 +1,59 @@ +// 2181. Merge Nodes in Between Zeros +#include "leetcode.h" + +/* + * given the 'head' of a linked list which contains a series of integers + * separated by 0's. the beginning and end of the linked list will have + * 'node.val == 0'. for every consecutive 0's, merge all the nodes lying in + * between them into a single node whose value is the sum of all the merged + * nodes. the modified list should not contain any 0's. return the 'head' of the + * modified linked list. + */ + +struct ListNode { + int val; + struct ListNode *next; +}; + +struct ListNode *mergeNodes(struct ListNode *head) { + head = head->next; + struct ListNode *trav = head; + while (trav) + if (trav->next && trav->next->val) { + trav->val = trav->val + trav->next->val; + trav->next = trav->next->next; + } else { + trav->next = trav->next->next; + trav = trav->next; + } + return head; +} + +struct ListNode *create_node(int val) { + struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode)); + new->val = val; + new->next = NULL; + return new; +} + +void print_node(struct ListNode *head) { + struct ListNode *curr = head; + while (curr != NULL) { + printf("%d ", curr->val); + curr = curr->next; + } + printf("\n"); +} + +int main() { + struct ListNode *head = create_node(0); + head->next = create_node(3); + head->next->next = create_node(1); + head->next->next->next = create_node(0); + head->next->next->next->next = create_node(4); + head->next->next->next->next->next = create_node(5); + head->next->next->next->next->next->next = create_node(2); + head->next->next->next->next->next->next->next = create_node(0); + struct ListNode *merged_head = mergeNodes(head); + print_node(merged_head); // expect: 4 11 +} diff --git a/merge_nodes_between_zero.py b/merge_nodes_between_zero.py new file mode 100644 index 0000000..7e730bd --- /dev/null +++ b/merge_nodes_between_zero.py @@ -0,0 +1,37 @@ +# 2181. Merge Nodes in Between Zeros + +""" +given the 'head' of a linked list which contains a series of integers +separated by 0's. the beginning and end of the linked list will have +'node.val == 0'. for every consecutive 0's, merge all the nodes lying in +between them into a single node whose value is the sum of all the merged +nodes. the modified list should not contain any 0's. return the 'head' of the +modified linked list. +""" + + +# Definition for singly-linked list. +class ListNode(object): + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution(object): + def mergeNodes(self, head): + """ + :type head: Optional[ListNode] + :rtype: Optional[ListNode] + """ + p1, p2 = head, head.next + s = 0 + while p2: + if p2.val == 0: + p1 = p1.next + p1.val = s + s = 0 + else: + s += p2.val + p2 = p2.next + p1.next = None + return head.next |