forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialising a LinkedList class, using a Node class to store the item and the next pointer.
- Loading branch information
1 parent
0dbd2df
commit 4a8fa8b
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Node: | ||
def __init__(self, item, next): | ||
self.item = item | ||
self.next = next | ||
|
||
class LinkedList: | ||
def __init__(self): | ||
self.head = None | ||
|
||
def add(self, item): | ||
self.head = Node(item, self.head) | ||
|
||
def remove(self): | ||
if self.is_empty(): | ||
return None | ||
else: | ||
item = self.head.item | ||
self.head = self.head.next | ||
return item | ||
|
||
def is_empty(self): | ||
return self.head == None |