Doubly Linked List in Python
Doubly Linked List in Python
Python
Lesson Plan
Doubly Linked List in Python
A Doubly Linked List in Python is similar to a singly linked list, except that each node also contains a pointer to
the previous node. This means that in a doubly linked list we can traverse not only in the forward direction but
also in the backward direction, which is not possible with a plain singly linked list.
Inserting a new node in a doubly linked list is very similar to inserting a new node in a singly linked list, with the
additional requirement to maintain the link of the previous node. A node can be inserted in a Doubly Linked List
in several ways
The new node is always added before the head of the given Linked List. The task can be performed by using the
following steps
class Node:
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
new_node = Node(new_data)
new_node.next = self.head
self.head.prev = new_node
self.head = new_node
node = node.next
print()
We are given a pointer to a node as prev_node, and the new node is inserted after the given node. This can be
done using the following steps
Allocate a new node
Put the data in the new node
Point the next of the new node to the next of prev_node
Point the next of prev_node to the new node
Point the prev of the new node to prev_node
Change the prev of the new node's next node.
if prev_node is None:
return
new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node
new_node.prev = prev_node
new_node.next.prev = new_node
Let the pointer to this given node be next_node. This can be done using the following steps
Allocate a new node
Put the data in the new node
Set the prev pointer of this new node to the prev of next_node
Set the prev pointer of the next_node to the new node
Set the next pointer of this new node to the next_node
Change the next of the new node's previous node.
if next_node is None:
return
new_node = Node(new_data)
new_node.prev = next_node.prev
next_node.prev = new_node
new_node.next = next_node
new_node.prev.next = new_node
else:
self.head = new_node
The new node is always added after the last node of the given Linked List. This can be done using the following
steps
Create a new node
Put the value in the new node
Make the next pointer of the new node as None
If the list is empty, make the new node as the head
Otherwise, travel to the end of the linked list
Now make the next pointer of the last node point to the new node.
Change the prev pointer of the new node to the last node of the list.
new_node = Node(new_data)
new_node.next = None
if self.head is None:
new_node.prev = None
self.head = new_node
return
last = self.head
last = last.next
last.next = new_node
new_node.prev = last
The deletion of a node in a doubly linked list can be divided into three main categories
Deletion of the head node
Deletion of a middle node
Deletion of the last node.
All three mentioned cases can be handled if the pointer to the node to be deleted and the head pointer are
known
If the node to be deleted is the head node, make the next node as head
If a node is deleted, connect the next and previous node of the deleted node.
def delete_node(self, del_node):
return
if self.head == del_node:
self.head = del_node.next
del_node.next.prev = del_node.prev
del_node.prev.next = del_node.next
# Driver Code
if __name__ == "__main__":
dll = DoublyLinkedList()
dll.push(2)
dll.push(4)
dll.push(8)
dll.push(10)
dll.print_list(dll.head)
dll.print_list(dll.head)
Output:
10 8 4 2
8
Complexity Analysis:
Since traversal of the linked list is not required, the time complexity is constant.
class ListNode:
self.val = val
self.next = next
class Solution:
cur = head
N = 0
while cur:
cur = cur.next
N += 1
ans = [None] * k
cur = head
for i in range(k):
head1 = ListNode(0)
write = head1
write.next = ListNode(cur.val)
write = write.next
if cur:
cur = cur.next
ans[i] = head1.next
return ans
class ListNode:
self.val = val
self.next = next
class Solution:
return ans
arr = []
t = head.next
prev = head
idx = 1
while t.next:
arr.append(idx)
arr.append(idx)
idx += 1
prev = t
t = t.next
if len(arr) < 2:
return ans
min_diff = float('inf')
ans[0] = min_diff
return ans
class ListNode:
self.val = val
self.next = next
class Solution:
while i < j:
i += 1
j -= 1
if not head.next:
return head
arr = []
t = head
while t:
arr.append(t.val)
t = t.next
k = 1
n = len(arr)
i = 0
while i < n:
j = min(n - 1, i + k - 1)
if (j - i + 1) % 2 == 0:
self.reverse(arr, i, j)
i = j + 1
k += 1
t = head
t.val = num
t = t.next
return head
class ListNode:
self.val = val
self.next = next
class Solution:
while i < j:
i += 1
j -= 1
if not head.next:
return head
arr = []
t = head
while t:
arr.append(t.val)
t = t.next
k = 1
n = len(arr)
i = 0
while i < n:
j = min(n - 1, i + k - 1)
if (j - i + 1) % 2 == 0:
self.reverse(arr, i, j)
i = j + 1
k += 1
t = head
t.val = num
t = t.next
return head
class Node:
self.val = val
self.prev = prev
self.next = next
self.child = child
class Solution:
if not head:
return None
current = head
stack = []
while current:
if current.child:
if current.next:
stack.append(current.next)
current.next = current.child
current.child.prev = current
current.child = None
next_node = stack.pop()
current.next = next_node
next_node.prev = current
current = current.next
return head