J
John Salerno
Can someone explain to me how this works:
def printBackward(list):
if list == None: return
head = list
tail = list.next
printBackward(tail)
print head,
3 2 1
The printable value of node1 is 1, node2 is 2 and node 3 is 3.
node1.next is node2, node2.next is node3 and node3.next is None.
This might be painfully obvious, but I don't understand when the print
statement is getting called. If you call printBackward with node1, then
you skip the if statement, head becomes node1, tail becomes node2 and
then you call printBackward again with node2. During this call you call
printBackward again with node 3 and then the next time the if statement
returns. So when does the print happen, and how does it print 3
different values? It seems like you wouldn't get to it until the last
time printBackward returns, and 'head' at that point would be 3, which
is the first number printed. But doesn't it stop at this point? Where do
2 and 1 come from?
Thanks!
def printBackward(list):
if list == None: return
head = list
tail = list.next
printBackward(tail)
print head,
3 2 1
The printable value of node1 is 1, node2 is 2 and node 3 is 3.
node1.next is node2, node2.next is node3 and node3.next is None.
This might be painfully obvious, but I don't understand when the print
statement is getting called. If you call printBackward with node1, then
you skip the if statement, head becomes node1, tail becomes node2 and
then you call printBackward again with node2. During this call you call
printBackward again with node 3 and then the next time the if statement
returns. So when does the print happen, and how does it print 3
different values? It seems like you wouldn't get to it until the last
time printBackward returns, and 'head' at that point would be 3, which
is the first number printed. But doesn't it stop at this point? Where do
2 and 1 come from?
Thanks!