1. 程式人生 > >python單鏈表

python單鏈表

1 \xrightarrow{} 2 \xrightarrow{}

3 \xrightarrow{} 4 \xrightarrow{}
5 \xrightarrow{} 6

listvalue[0]必須為頭
法一:

listvalue = [1,3,6,2,4,5]
listpointer = [3,4,-1,1,5,2]
head = 0
print(listvalue[head])
next
= listpointer[head] while next != -1: print(listvalue[next]) next = listpointer[next]

法二:

linkedlist = [[1, 3], [3, 4], [6, -1], [2, 1], [4, 5], [5, 2]]
value = 0
pointer = 1
head = 0
print(linkedlist[head][value])
next = linkedlist[head][pointer]
while next != -1:
    print(linkedlist[next][value])
    next = linkedlist[next][pointer]