https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Linked%20Lists/Linked%20Lists%20Interview%20Problems/Linked%20List%20Interview%20Problems%20-%20SOLUTIONS/Implement%20a%20Linked%20List%20-SOLUTION.ipynb

Implement a Linked List - SOLUTION

Problem Statement

Implement a Linked List by using a Node class object. Show how you would implement a Singly Linked List and a Doubly Linked List!

Solution

Since this is asking the same thing as the implementation lectures, please refer to those video lectures and notes for a full explanation. The code from those lectures is displayed below:

 

Singly Linked List

In [1]:
class LinkedListNode(object):

    def __init__(self,value):

        self.value = value
self.nextnode = None
In [2]:
a = LinkedListNode(1)
b = LinkedListNode(2)
c = LinkedListNode(3)
In [3]:
a.nextnode = b
b.nextnode = c
 

Doubly Linked List

In [4]:
class DoublyLinkedListNode(object):

    def __init__(self,value):

        self.value = value
self.next_node = None
self.prev_node = None
In [5]:
a = DoublyLinkedListNode(1)
b = DoublyLinkedListNode(2)
c = DoublyLinkedListNode(3)
In [6]:
# Setting b after a
b.prev_node = a
a.next_node = b
In [7]:
# Setting c after a
b.next_node = c
c.prev_node = b
 

Good Job!

05-11 10:59