Skip to content

Instantly share code, notes, and snippets.

@qkreltms
Created March 15, 2018 13:00
Show Gist options
  • Select an option

  • Save qkreltms/9a13b7c934d0ed11fa8b77e82ac96732 to your computer and use it in GitHub Desktop.

Select an option

Save qkreltms/9a13b7c934d0ed11fa8b77e82ac96732 to your computer and use it in GitHub Desktop.
liked_list python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.tail = None
self.head = None
self.cur = None
self.pre = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
self.tail.next = None
def pop(self):
self.pre = self.head
if self.head == self.tail:
self.head = None
return self.tail.data
while self.pre.next is not self.tail:
self.pre = self.pre.next
data = self.tail.data
self.tail = self.pre
self.tail.next = None
return data
def insert(self, find, data):
self.cur = self.head
while True:
if self.cur == None:
return
if self.cur.data == find:
break
self.cur = self.cur.next
temp = self.cur.next
self.cur.next = Node(data)
self.cur.next.next = temp
linked_list = LinkedList()
linked_list.append("test")
linked_list.append("test2")
linked_list.append("test3")
linked_list.insert("test2", "test4")
print(linked_list.pop())
print(linked_list.pop())
print(linked_list.pop())
print(linked_list.pop())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment