How to create linked list automatically

Peter Otten __peter__ at web.de
Tue Dec 20 13:07:44 CET 2005


Shahriar Shamil Uulu wrote:

> Thank you very much to all,
> we have figured it out, how to make it work,
> w=[]
> for i in range(10):
>      node=Node(i)
>      w.append(node)
> 
> for i in range(10):
>     a=w[i]
>     if i+1>9:
>            b=w[9]
>            a.next=b
>     else:
>            b=w[i+1]
>            a.next=b
> 
> we have runned in this way

The last for-loop can be simplified to

for i in range(9):
    w[i].next = w[i+1]
w[9].next = w[9]

Are you sure you want the last node to point to itself? The printNodes()
function you posted above will enter an infinite loop...

Peter



More information about the Python-list mailing list