blob: 5a9a169d8946e137099be306488b272e6dee4b23 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
// 1171. Remove Zero Sum Consecutive Nodes from Linked List
#include "leetcode.h"
/*
* given the 'head' of a linked list, we repeatedly delete the consecutive
* sequences of nodes that sum to 0 until there are no such sequences. after
* doing so, return the head of the final linked list. you may return any such
* answer.
*/
// Definition for singly-linked list.
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *get_sum(struct ListNode *head) {
struct ListNode *node = head;
int sum = 0;
while (node) {
sum += node->val;
if (!sum)
return node;
node = node->next;
}
return node;
}
struct ListNode *removeZeroSumSublists(struct ListNode *head) {
struct ListNode *tmp, *node, *prev, *ans;
tmp = (struct ListNode *)malloc(sizeof(struct ListNode));
tmp->val = -2000;
tmp->next = head;
while (1) {
prev = tmp;
node = tmp->next;
int n = 0;
while (node) {
ans = get_sum(node);
if (ans) {
prev->next = ans->next;
n = 1;
break;
} else {
prev = node;
node = node->next;
}
}
if (!n)
break;
}
return tmp->next;
}
|