
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self, value):
self.head = Node(value)
def append(self, value):
cur = self.head
while cur.next is not None:
cur = cur.next
cur.next = Node(value)
def get_kth_node_from_last(self, k):
length = 1
cur = self.head
while cur.next is not None:
cur = cur.next
length += 1
end_length = length - k
cur = self.head
for i in range(end_length):
cur = cur.next
return cur
linked_list = LinkedList(6)
linked_list.append(7)
linked_list.append(8)
print(linked_list.get_kth_node_from_last(2).data)
문제를 풀기에 앞서 해결하기 위한 방법에 대한 색다른 생각이 필요할 것 같다.
시간복잡도를 확인해보니 생각과는 다르게 큰 변화가 없었다.
문법에 대하여 다시 정리해보아야 겠다.