★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:为敢(WeiGanTechnologies)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ndefhpoo-kc.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given the head
of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0
until there are no such sequences.node
After doing so, return the head of the final linked list. You may return any such answer.git
(Note that in the examples below, all sequences are serializations of ListNode
objects.)github
Example 1:微信
Input: head = [1,2,-3,3,1] Output: [3,1] Note: The answer [1,2,1] would also be accepted.
Example 2:app
Input: head = [1,2,3,-3,4] Output: [1,2,4]
Example 3:spa
Input: head = [1,2,3,-3,-2] Output: [1]
Constraints:code
1
and 1000
nodes.-1000 <= node.val <= 1000
.给你一个链表的头节点 head
,请你编写代码,反复删去链表中由 总和 值为 0
的连续节点组成的序列,直到不存在这样的序列为止。htm
删除完毕后,请你返回最终结果链表的头节点。对象
你能够返回任何知足题目要求的答案。
(注意,下面示例中的全部序列,都是对 ListNode
对象序列化的表示。)
示例 1:
输入:head = [1,2,-3,3,1] 输出:[3,1] 提示:答案 [1,2,1] 也是正确的。
示例 2:
输入:head = [1,2,3,-3,4] 输出:[1,2,4]
示例 3:
输入:head = [1,2,3,-3,-2] 输出:[1]
提示:
1
到 1000
个节点。-1000 <= node.val <= 1000
.1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeZeroSumSublists(_ head: ListNode?) -> ListNode? { 14 var head = head 15 if head == nil {return head} 16 var sums:[Int:ListNode] = [Int:ListNode]() 17 var sum:Int = 0 18 var curr:ListNode? = head 19 while(curr != nil) 20 { 21 sum += curr!.val 22 if sum == 0 23 { 24 head = curr?.next 25 } 26 if sums[sum] != nil 27 { 28 sums[sum]!.next = curr?.next 29 } 30 else 31 { 32 sums[sum] = curr 33 } 34 curr = curr?.next 35 } 36 return head 37 } 38 }
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeZeroSumSublists(_ head: ListNode?) -> ListNode? { 14 15 var arr = [Int]() 16 var sumArr = [Int]() 17 var dic = [0: -1] 18 var head = head 19 var invaildRange = [(Int, Int)]() 20 var i = 0 21 while head != nil { 22 if head!.val == 0 { 23 head = head!.next 24 continue 25 } 26 arr.append(head!.val) 27 if sumArr.count == 0 { 28 sumArr.append(head!.val) 29 } else { 30 sumArr.append(sumArr[sumArr.count-1]+head!.val) 31 } 32 if dic[sumArr[sumArr.count-1]] != nil { 33 let lasti = dic[sumArr[sumArr.count-1]]! 34 invaildRange.append((lasti+1, i)) 35 for j in lasti..<i { 36 dic[sumArr[j+1]] = nil 37 } 38 dic[sumArr[sumArr.count-1]] = lasti 39 } else { 40 dic[sumArr[sumArr.count-1]] = i 41 } 42 head = head!.next 43 i += 1 44 } 45 if sumArr.count == 0 || sumArr[sumArr.count-1] == 0 { 46 return nil 47 } 48 49 // print(invaildRange) 50 51 var hd: ListNode? = nil 52 var res: ListNode? = nil 53 54 invaildRange.append((Int.max, Int.max)) 55 invaildRange.sort { 56 if $0.0 < $1.0 || ($0.0 == $1.0 && $0.1 > $1.1 ) { 57 return true 58 } 59 return false 60 } 61 var last = invaildRange.removeFirst() 62 for i in arr.indices { 63 while i > last.1 && invaildRange.count > 0 { 64 last = invaildRange.removeFirst() 65 } 66 // print(i, last) 67 if i < last.0 { 68 if hd == nil { 69 hd = ListNode(arr[i]) 70 res = hd 71 } else { 72 hd!.next = ListNode(arr[i]) 73 hd = hd!.next 74 } 75 } 76 } 77 return res 78 } 79 }
44ms
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeZeroSumSublists(_ head: ListNode?) -> ListNode? { 14 var arr = [Int](), sumArr = [Int]() 15 var dic = [0: -1] 16 var head = head 17 var invaildRange = [(Int, Int)]() 18 var i = 0 19 while head != nil { 20 //ignor zero element 21 if head!.val == 0 { head = head!.next; continue } 22 23 arr.append(head!.val) 24 if sumArr.count == 0 { sumArr.append(head!.val) } 25 else { sumArr.append(sumArr.last! + head!.val) } 26 27 let lasti = dic[sumArr.last!] ?? i 28 if lasti != i { 29 invaildRange.append((lasti+1, i)) 30 for j in lasti..<i { 31 dic[sumArr[j+1]] = nil 32 } 33 } 34 dic[sumArr.last!] = lasti 35 head = head!.next 36 i += 1 37 } 38 if sumArr.count == 0 || sumArr[sumArr.count-1] == 0 { return nil } 39 var hd: ListNode? = nil 40 var res: ListNode? = nil 41 42 invaildRange.append((Int.max, Int.max)) 43 invaildRange.sort { $0.0 < $1.0 } 44 var last = invaildRange.removeFirst() 45 for i in arr.indices { 46 while i > last.1 { last = invaildRange.removeFirst() } 47 if i < last.0 { 48 if hd == nil { 49 hd = ListNode(arr[i]) 50 res = hd 51 } else { 52 hd!.next = ListNode(arr[i]) 53 hd = hd!.next 54 } 55 } 56 } 57 return res 58 } 59 }