wmpvuq
Last Updated: February 25, 2016
·
1.395K
· purak
Fe0a1519fcf6d2efd7f1a0bbc20660d2

Doubly Linked List in Python

A Doubly Linked List implementation in Python:

class LinkedList(object):
    __slots__ = 'prev', 'next', 'value'

ll1 = LinkedList()
ll2 = LinkedList()
ll3 = LinkedList()

ll1.value = "Value 1"
ll1.next = ll2
ll1.prev = None


ll2.value = "Value 2"
ll2.next = ll3
ll2.prev = ll1


ll3.value = "Value 3"
ll3.next = None
ll3.prev = ll2

print("Linearly: \n")
print ll1.value
print ll1.next.value
print ll1.next.next.value

print("Reversely: \n")
print ll3.value
print ll3.prev.value
print ll3.prev.prev.value

Run the code and get the following:

Linearly: 

Value 1
Value 2
Value 3

Reversely: 

Value 3
Value 2
Value 1

Nice!

Say Thanks
Respond