1. 程式人生 > >python連結串列計數實現

python連結串列計數實現

class Node(object):
def init(self, val, next=None):
self.val = val
self.next = next

定義節點,變數都是指標,賦值直接self.val = val。但是這裡的含義還有點不太清楚

class Solution:
def Forlength(self,head):
cur = head
count = 0
while cur:
count += 1
cur = cur.next
return count