File: root - text - article - 2019 - 11 - Remove-Consecutive-Nodes-that-Sum-to-0.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 462 Views, 25847 Search Bots | 146 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 462 Views, 25847 Search Bots | 146 Words
| Browse | Archive
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:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 462 Views, 25847 Search Bots | 146 Words 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
Related Articles
- Daily Interview Problem: Running Median
- Daily Interview Problem: Largest Product of 3 Elements I
- Longest Substring Without Repeating Characters
- Daily Interview Problem: Deepest Node in a Binary Tree
- Daily Interview Problem: Tree Serialization
- Two Tricks of Delphi
- Absolute Path
- Reverse a Linked List
- Daily Interview Problem: Queue Using Two Stacks
- Daily Interview Problem: Trapping Rainwater
©2006~2024 SteakOverCooked - 0.00943 Seconds(s) - 2107.962 KB/s - 12 Online Memory: 493.4 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !