Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
initial linked list solution
  • Loading branch information
estoup committed Oct 22, 2020
commit ec4069195851fcbbb7fd0dc53883ca718f520aba
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
64 changes: 56 additions & 8 deletions python/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,67 @@
class LinkList:
# write your __init__ method here that should store a 'head' value which the first Node in the LinkedList and a 'length' value which is the total number of Nodes in the LinkedList
def __init__(self, head=None):
self.head = head
self.length = 0

def add(self, data):
# write your code to ADD an element to the Linked List
pass
if not self.head:
new_node = Node(data)
self.head = new_node
else:
previous_node = self.head
while previous_node.next_node:
current_node = previous_node.next_node
previous_node = current_node
new_node = Node(data)
previous_node.next_node = new_node
self.length += 1


def remove(self, data):
# write your code to REMOVE an element from the Linked List
pass
if self.head.data == data:
self.head = self.head.next_node
else:
previous_node = self.head
current_node = self.head.next_node
while current_node.data != data:
previous_node = current_node
current_node = current_node.next_node
previous_node.next_node = current_node.next_node
self.length -= 1


def get(self, element_to_get):
# write you code to GET and return an element from the Linked List
pass
current_node = self.head
for _i in range(0, element_to_get):
current_node = current_node.next_node
return current_node.data


# ----- Node ------
class Node:
# store your DATA and NEXT values here
pass

def __init__(self, data, next_node=None):
self.data = data
self.next_node=next_node


ll = LinkList()
ll.add(1)
ll.add(2)
ll.add(3)

print(ll.head)
print(ll.head.data)
print(ll.head.next_node.data)
print(ll.head.next_node.next_node.data)
print(ll.length)

# ll.remove(3)

print(ll.head.data)
print(ll.head.next_node.data)
print(ll.length)

print(ll.get(0))