To the Top
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 | 164 Views, 18777 Search Bots | 146 Words

Subscribe to Feed Burner | 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:


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
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 164 Views, 18777 Search Bots | 146 Words Subscribe to Feed Burner

Related Articles

  1. [Daily Problem] Remove Consecutive Nodes that Sum to 0
  2. Daily Interview Problem: Product of Array Except Self
  3. Daily Interview Question: Longest Sequence with Two Unique Numbers
  4. [Daily Problem] Angles of a Clock
  5. Algorithm Interview: Convert Roman Numerals to Decimal
  6. Invert a Binary Tree
  7. Daily Interview Problem: Find the Number of Islands
  8. Daily Interview Problem: Validate Binary Search Tree
  9. Find the non-duplicate number
  10. Binary Tree Level with Minimum Sum

Comments (0)

    Be the first one to comment this page !


Page Edited: May 11 2024 14:36:49 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />