Link up

What will the function below output when it is run on the following linked list a : 3 2 1 0 1 2 3 4 5 a : -3\rightarrow-2\rightarrow-1 \rightarrow 0 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5

1
2
3
4
5
6
7
8
def fun2(node):
    if node == None:
        return 1
    funny = 1
    if node.next != None:
        funny = funny * fun2( node.next.next )
    funny *= node.data
    return funny

Details and Assumptions

  • In other words, what is the output of print fun2(a) ?


The answer is 45.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Hasmik Garyaka
Sep 13, 2017

Product of all odd elements.

Bill Bell
Oct 22, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## https://brilliant.org/problems/link-up/
## approach due to http://en.literateprograms.org/Singly_linked_list_(Python)

class ListNode:
    def __init__ (self, data, next):
        self.data = data
        self.next = next

def fun2(node):
    if node == None:
        return 1
    funny = 1
    if node.next != None:
        funny = funny * fun2( node.next.next )
    funny *= node.data
    return funny

items=[-3, -2, -1, 0, 1, 2, 3 , 4, 5][::-1]
p=None
for item in items:
    p=ListNode(item,p)
a=p

print fun2(a)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...