-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6. LinkedList.py
More file actions
89 lines (71 loc) · 2.62 KB
/
6. LinkedList.py
File metadata and controls
89 lines (71 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class Node:
def __init__(self, item):
self.data = item
self.next = None
class LinkedList:
def __init__(self):
self.nodeCount = 0
self.head = None
self.tail = None
def __repr__(self):
if self.nodeCount == 0:
return 'LinkedList: empty'
s = ''
curr = self.head
while curr is not None:
s += repr(curr.data)
if curr.next is not None:
s += ' -> '
curr = curr.next
return s
def getAt(self, pos):
if pos < 1 or pos > self.nodeCount:
return None
i = 1
curr = self.head
while i < pos:
curr = curr.next
i += 1
return curr
def insertAt(self, pos, newNode):
if pos < 1 or pos > self.nodeCount + 1:
raise IndexError
if pos == 1: # 첫번째 인덱스에 삽입하는 경우는 그냥 head만 갈켜주면 된다. 그래서 이전 노드가 필요없다.
newNode.next = self.head # 데이터를 처음으로 추가할 때도 이 코드로 커버 가능하다.
self.head = newNode
else: # 나머지 인덱스의 경우는 <이전노드의 링크도 바꿔야>하기 때문에 이전 노드가 필요하다.
if pos == self.nodeCount + 1: # 끝에 추가할 경우엔
prev = self.tail # 굳이 getAt사용할 필요 X
else:
prev = self.getAt(pos - 1)
newNode.next = prev.next
prev.next = newNode
if pos == self.nodeCount + 1: # 맨끝에 추가할 경우는 특히 tail을 바꿔줌
self.tail = newNode
self.nodeCount += 1
return True
def popAt(self, pos):
if pos < 1 or self.nodeCount < pos:
raise IndexError
if pos == 1: # 데이터가 1개일 때도 이 코드로 커버 가능하다. 그냥 None을 head로 놓는거니까
curr = self.head
self.head = curr.next # pop이니까 2번째 노드를 head로
if pos == self.nodeCount: # pos==1 && pos==self.nodeCount는 노드갯수가 1개뿐이라는 의미
self.tail = None
else:
prev = self.getAt(pos-1)
curr = prev.next
prev.next = curr.next
if pos == self.nodeCount:
self.tail = prev
self.nodeCount -= 1
return curr.data
def traverse(self):
result = []
curr = self.head
while curr is not None:
result.append(curr.data)
curr = curr.next
return result
def solution(x):
return 0