본문 바로가기
DC 2

[자료구조] (singly) Linked List 파이썬

by YGSEO 2021. 4. 20.
728x90

연결리스트를 끝까지 순회하는 방법

 

LeetCode 같은 경우

# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

 

def traverse(self):
        if self.head == None:
            return []
        curr = self.head
        res = []
        
        while curr:
            res.append(curr.data)
            curr = curr.next
        return res
728x90

댓글