Profile picture
Alvin ZablanFounder at Structy and React Formula | ex-Google

Sum List Approach

In this lesson, Alvin explores the strategy to solving the following interview problem:

Write a function, sum_list, that takes in the head of a linked list containing numbers as an argument. The function should return the total sum of all values in the linked list.

a = Node(2)
b = Node(8)
c = Node(3)
d = Node(-1)
e = Node(7)
a.next = b
b.next = c
c.next = d
d.next = e

# 2 -> 8 -> 3 -> -1 -> 7
sum_list(a) # 19
x = Node(38)
y = Node(4)
x.next = y

# 38 -> 4
sum_list(x) # 42
z = Node(100)

# 100
sum_list(z) # 100

If you need additional support taking these DSA skills and actually applying them, take Alvin's complete data structures and algorithms course on Structy. You can try out the concepts yourself in their interactive code editor and learn advanced DSA patterns like stack exhaustive recursion.

Use this link to get 20% off the entire Structy DSA learning experience.

Follow Alvin on LinkedIn: https://www.linkedin.com/in/alvin-zablan-b73a92117/