반응형
linked list
linked list는 이런 노드가 연결 되어있다.
class Node:
def __init__(self, item):
self.val = item
self.next = None
class LinkedList:
def __init__(self, item):
self.head = Node(item)
def add(self, item):
cur = self.head
while cur.next != None:
cur = cur.next
cur.next = Node(item)
def remove(self, item):
if self.head.val == item:
self.head = self.head.next
else:
cur = self.head
while cur.next != None:
if cur.next == item:
self._remove(item)
return
cur = cur.next
def _remove(self, item):
cur = self.head
while cur.next != None:
if cur.next.val == item:
nextnode = cur.next.next
cur.next = nextnode
break
def reverse(self):
prev = None
cur = self.head
while cur != None:
next = cur.next
cur.next = prev
prev = cur
cur = next
self.head = prev
def print(self):
cur = self.head
link = []
while cur != None:
link.append(cur.val)
cur = cur.next
print(link)
반응형
'Data > python·알고리즘' 카테고리의 다른 글
[Binary Tree] 이진트리 (1) - 삽입, 삭제 (0) | 2018.09.24 |
---|---|
[heap sort] 힙 힙 힙! (0) | 2018.09.24 |
[큐와 스택] queue와 stack (0) | 2018.09.24 |
[python] jupyter notebook 코드를 tistory에 올리는 법. (0) | 2018.09.16 |
[python] 알아두면 좋은 Standard Library (0) | 2018.06.29 |
댓글