码迷,mamicode.com
首页 > 其他好文 > 详细

leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List

时间:2020-02-01 23:18:44      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:ram   部分   节点   twosum   没有   遍历   连续   head   sel   

  1 """
  2 Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.
  3 
  4 After doing so, return the head of the final linked list.  You may return any such answer.
  5 
  6  
  7 
  8 (Note that in the examples below, all sequences are serializations of ListNode objects.)
  9 
 10 Example 1:
 11 
 12 Input: head = [1,2,-3,3,1]
 13 Output: [3,1]
 14 Note: The answer [1,2,1] would also be accepted.
 15 
 16 Example 2:
 17 
 18 Input: head = [1,2,3,-3,4]
 19 Output: [1,2,4]
 20 
 21 Example 3:
 22 
 23 Input: head = [1,2,3,-3,-2]
 24 Output: [1]
 25 
 26 """
 27 class ListNode(object):
 28     def __init__(self, x):
 29         self.val = x
 30         self.next = None
 31 
 32 class Solution1(object):
 33     def removeZeroSumSublists(self, head):
 34         """
 35         :param head: ListNode
 36         :return: ListNode
 37         """
 38         if not head.next:
 39             return head if head.val != 0 else None  #判断头节点是为否为空
 40         list = []   #建立list存储链表转化后的数组
 41         p = head    #建立p指针指向头结点
 42         while(p):   #将链表转为数组
 43             list.append(p.val)
 44             p = p.next
 45         list = self.remove(list)  #!!!删除连续和为0
 46         newhead = ListNode(-1)    #建立新的头结点
 47         p = newhead               #p指向新的头结点
 48         for num in list:          #将结果数组转成链表
 49             p.next = ListNode(num)
 50             p = p.next
 51         return newhead.next
 52     """
 53     在一个数组里把连续和为0的部分删除,两层循环:用i对每个元素遍历
 54     再用j不断的对当前子数组求和,若为0,删除当前部分并进行递归
 55     """
 56     def remove(self, list):       #在一个数组里把连续和为0的部分删除
 57         for i in range(len(list)):
 58             sum = list[i]
 59             j = i + 1
 60             while(j <= len(list)):
 61                 if sum == 0:
 62                     return self.remove(list[:i] + list[j:])   #递归处理
 63                 else:
 64                     if j == len(list):
 65                         break
 66                     sum += list[j]
 67                     j += 1
 68         return list
 69 
 70 """
 71 用一个变量pre_sum记录前缀和,再用一个哈希表记录出现过的前缀和,
 72 如果出现了两个相同的前缀和,就说明中间这一段的和为0,是多余的。
 73 举例:
 74 对于输入 [1, 2, -2, 3, -1, -1, -1],
 75 前缀和为[1, 3, 1, 4, 3, 2, 1],
 76 下标为0的1和下标为2的1相同,
 77 就代表下标在【1, 2】间的元素的和为0。
 78 """
 79 class Solution2(object):
 80     def removeZeroSumSublists(self, head):
 81         """
 82         :param head: ListNode
 83         :return: ListNode
 84         """
 85         dummy = ListNode(-1) #用一个假头结点dummy返回结果,
 86         dummy.next = head    #防止头节点head被删除无法返回
 87         pre_sum = 0          #记录前缀和
 88         record = {0: dummy}  # 用dict来存出现过的每个前缀和
 89         # bug代码record = {0,dummy} 这里需要对record初始化{:}
 90         while head:
 91             pre_sum += head.val  # bug代码 马虎没有写‘+‘
 92             if pre_sum in record:
 93                 record[pre_sum].next = head.next #寻找是否有重复的元素
 94             else:                                #类似于leetcode1:twoSum
 95                 record[pre_sum] = head
 96             head = head.next
 97         return dummy.next    #用dummy来返回结果
 98 """
 99 Wrong answer
100 Input
101 [1,3,2,-3,-2,5,5,-5,1]
102 Output
103 [1,5,5,-5,1]
104 Expected
105 [1,5,1]
106 """

 

leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List

标签:ram   部分   节点   twosum   没有   遍历   连续   head   sel   

原文地址:https://www.cnblogs.com/yawenw/p/12250481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!