mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added linked list class.
This commit is contained in:
parent
47703cde50
commit
6f3e3b0b8f
75
linked-lists/linked_list.py
Normal file
75
linked-lists/linked_list.py
Normal file
|
@ -0,0 +1,75 @@
|
|||
|
||||
class Node(object):
|
||||
def __init__(self, data):
|
||||
self.next = None
|
||||
self.data = data
|
||||
|
||||
def __str__(self):
|
||||
return self.data
|
||||
|
||||
class LinkedList(object):
|
||||
def __init__(self, head):
|
||||
self.head = head
|
||||
|
||||
def __len__(self):
|
||||
curr = self.head
|
||||
counter = 0
|
||||
while curr is not None:
|
||||
counter += 1
|
||||
curr = curr.next
|
||||
return counter
|
||||
|
||||
def insert_to_front(self, data):
|
||||
if data is None:
|
||||
return
|
||||
node = Node(data)
|
||||
if self.head is None:
|
||||
self.head = node
|
||||
else:
|
||||
node.next = self.head
|
||||
self.head = node
|
||||
|
||||
def append(self, data):
|
||||
if data is None:
|
||||
return
|
||||
node = Node(data)
|
||||
if self.head is None:
|
||||
self.head = node
|
||||
else:
|
||||
curr_node = self.head
|
||||
while curr_node.next is not None:
|
||||
curr_node = curr_node.next
|
||||
curr_node.next = node
|
||||
|
||||
def find(self, data):
|
||||
if data is None:
|
||||
return
|
||||
if self.head is None:
|
||||
return
|
||||
curr_node = self.head
|
||||
while curr_node is not None:
|
||||
if curr_node.data == data:
|
||||
return curr_node
|
||||
else:
|
||||
curr_node = curr_node.next
|
||||
|
||||
def delete(self, data):
|
||||
if data is None:
|
||||
return
|
||||
if self.head is None:
|
||||
return
|
||||
prev_node = self.head
|
||||
curr_node = prev_node.next
|
||||
while curr_node is not None:
|
||||
if curr_node.data == data:
|
||||
prev_node.next = curr_node.next
|
||||
return
|
||||
else:
|
||||
prev_node = curr_node
|
||||
curr_node = curr_node.next
|
||||
|
||||
def print_list(self):
|
||||
curr_node = self.head
|
||||
while curr_node is not None:
|
||||
print(curr_node.data)
|
||||
curr_node = curr_node.next
|
Loading…
Reference in New Issue
Block a user