2

Hey everyone, can someone explain to me why the array problem works but the count problem doesn't?

Write a function, linked_list_values, that takes in the head of a linked list as an argument. The function should return a list containing all values of the nodes in the linked list.

def linked_list_values(head):
  result = []
  recursive_approach(head, result)
  return result
  
def recursive_approach(head, result):
  if head is None:
    return
  result.append(head.val)
  recursive_approach(head.next, result)

a = Node("a")
b = Node("b")
c = Node("c")
d = Node("d")

a.next = b
b.next = c
c.next = d

# a -> b -> c -> d

linked_list_values(a) # -> [ 'a', 'b', 'c', 'd' ]

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.

def sum_list(head):
  count = 0
  add_nums(head, count)
  return count

def add_nums(head, count):
  if head is None:
    return 
  count += head.val
  add_nums(head.next, count)

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

Actual answer = 0

Definition of Node:

class Node:
  def __init__(self, val):
    self.val = val
    self.next = None
InSync
  • 4,851
  • 4
  • 8
  • 30

2 Answers2

0

you are not returning any values in the add_nums function, you need to return count by default if head is None and return the sum of the values otherwise (see below)

def add_nums(head, count):
  if head is None:
    return count
  return add_nums(head.next, head.val + count)

I didn't notice before, but you also don't assign count in sum_list

def sum_list(head):
  return add_nums(head, 0)
iggy12345
  • 1,233
  • 12
  • 31
  • The person asking the question got burned by python's call-by-object reference and that fact that mutable objects are available outside of the function, while immutable objects (integers) are not. The solution @igg12345 proposes is good. – WombatPM Jun 14 '23 at 15:24
0

If you add return count to your add_nums function it will work. Of course, you will also need to assign the value count, at all other calls like in function sum_list().

class Node:
   def __init__(self, dataval=None):
      self.val = dataval
      self.next = None

def sum_list(head):
  count = 0
  count = add_nums(head, count)
  return count

def add_nums(head, count):
  if head is None:
    return count 
  count += head.val
  count = add_nums(head.next, count)
  return count

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

print(sum_list(a)) # Output 19
Nathan
  • 83
  • 5
  • While I applaud you for answering this question, you should avoid giving an entire solution to somebody who is clearly asking about school work, this forces them to learn to modify their own code instead of plagiarizing your entire solution – iggy12345 Jun 13 '23 at 17:44
  • @iggy12345, StackOverflow is a Q&A site, that should mainly serve future visitors. Providing the full answer is fine. – trincot Jun 13 '23 at 17:46
  • @trincot StackOverflow is a site utilized vastly by students looking to get out of doing their own school work, and this should be discouraged – iggy12345 Jun 13 '23 at 17:47
  • We don't care about that. That is their responsibility. Our aim is to provide a large base of answers. If you feel that the asker doesn't deserve an easy way to copy/paste an answer, then refrain from answering, but don't discourage others to answer if they want to. – trincot Jun 13 '23 at 17:47
  • I mean if you take stack overflow's motto at face value maybe, but if you dig deeper, the site is not about doing other people's homework, it's about tutoring and fostering understanding – iggy12345 Jun 13 '23 at 17:52
  • I'm not discouraging this person from answering, I'm politely asking them not to encourage cheating by giving a clear answer, with partial code, like I did below – iggy12345 Jun 13 '23 at 17:53
  • They don't encourage cheating. It is fine. – trincot Jun 13 '23 at 17:54
  • 1
    Its a good debate, I hope having the answer will help him understand. Sometimes I find it more usefull to look at the solution directly, but I do understand your point. – Nathan Jun 13 '23 at 17:55