文件: root - text - article - 2019 - 11 - Remove-Consecutive-Nodes-that-Sum-to-0.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 442 次阅读, 24507 次搜索 | 146 个单词
| 浏览 | 博客存档
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 442 次阅读, 24507 次搜索 | 146 个单词
| 浏览 | 博客存档
Hi, here's your problem today. This problem was recently asked by Uber:
Given a linked list of integers, remove all consecutive nodes that sum up to 0.
Example:
Input: 10 -> 5 -> -3 -> -3 -> 1 -> 4 -> -4
Output: 10
The consecutive nodes 5 -> -3 -> -3 -> 1 sums up to 0 so that sequence should be removed. 4 -> -4 also sums up to 0 too so that sequence should also be removed.
Here's a starting point:
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 442 次阅读, 24507 次搜索 | 146 个单词 Given a linked list of integers, remove all consecutive nodes that sum up to 0.
Example:
Input: 10 -> 5 -> -3 -> -3 -> 1 -> 4 -> -4
Output: 10
The consecutive nodes 5 -> -3 -> -3 -> 1 sums up to 0 so that sequence should be removed. 4 -> -4 also sums up to 0 too so that sequence should also be removed.
Here's a starting point:
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def removeConsecutiveSumTo0(node):
# Fill this in.
node = Node(10)
node.next = Node(5)
node.next.next = Node(-3)
node.next.next.next = Node(-3)
node.next.next.next.next = Node(1)
node.next.next.next.next.next = Node(4)
node.next.next.next.next.next.next = Node(-4)
node = removeConsecutiveSumTo0(node)
while node:
print node.value,
node = node.next
# 10
猜您喜欢...
- Two-Sum
- Daily Interview Problem: Merge Overlapping Intervals
- ImageMagick Vulnerabilities - CVE-2016–3714
- Daily Interview Question: Word Search
- Daily Interview Problem:Create a balanced binary search tree
- Daily Interview Problem: Min Stack
- Daily Interview Problem: Find the Number of Islands
- Algorithm Interview: Shifted String
- [Daily Problem] Longest Palindromic Substring
- [Daily Problem] Validate Balanced Parentheses
©2006~2024 牛排过熟 - 0.00957 秒 - 2209.686 KB/s - 33 在线 内存: 495.41 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
牛排怎么做才好吃? | <meta name="机器人" content="索引, 跟踪">
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
评论 (0)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。