Banana-Leopard/TASTY huggingface dataset:
# Program to delete a node in a doubly-linked list
 
# for Garbage collection
import gc
 
# A node of the doubly linked list
class Node:
     
    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None
 
class DoublyLinkedList:
     # Constructor for empty Doubly Linked List
    def __init__(self):
        self.head = None
  
   # Function to delete a node in a Doubly Linked List.
   # head_ref --> pointer to head node pointer.
   # dele --> pointer to node to be deleted
 
    def deleteNode(self, dele):
         
        # Base Case
        if self.head is None or dele is None:
            return
         
        # If node to be deleted is head node
        if self.head == dele:
            self.head = dele.next
 
        # Change next only if node to be deleted is NOT
        # the last node
        if dele.next is not None:
            dele.next.prev = dele.prev
     
        # Change prev only if node to be deleted is NOT
        # the first node
        if dele.prev is not None:
            dele.prev.next = dele.next
        # Finally, free the memory occupied by dele
        # Call python garbage collector
        gc.collect()
 
 
    # Given a reference to the head of a list and an
    # integer, inserts a new node on the front of list
    def push(self, new_data):
  
        # 1. Allocates node
        # 2. Put the data in it
        new_node = Node(new_data)
  
        # 3. Make next of new node as head and
        # previous as None (already None)
        new_node.next = self.head
  
        # 4. change prev of head node to new_node
        if self.head is not None:
            self.head.prev = new_node
  
        # 5. move the head to point to the new node
        self.head = new_node
 
 
    def printList(self, node):
        while(node is not None):
            print(node.data,end=' ')
            node = node.next
 
 
# Driver program to test the above functions
 
# Start with empty list
dll = DoublyLinkedList()
 
# Let us create the doubly linked list 10<->8<->4<->2
dll.push(2);
dll.push(4);
dll.push(8);
dll.push(10);
 
print ("\n Original Linked List",end=' ')
dll.printList(dll.head)
 
# delete nodes from doubly linked list
dll.deleteNode(dll.head)
dll.deleteNode(dll.head.next)
dll.deleteNode(dll.head.next)
# Modified linked list will be NULL<-8->NULL
print("\n Modified Linked List",end=' ')
dll.printList(dll.head)
 
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

Time complexity: constant
Space complexity: constant
# Python3 Program to reverse a doubly linked list
 
# A node of the doubly linked list
 
class Node:
 
    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None
 
 
class DoublyLinkedList:
     # Constructor for empty Doubly Linked List
    def __init__(self):
        self.head = None
 
    # Function reverse a Doubly Linked List
    def reverse(self):
        temp = None
        current = self.head
 
        # Swap next and prev for all nodes of
        # doubly linked list
        while current is not None:
            temp = current.prev
            current.prev = current.next
            current.next = temp
            current = current.prev
 
        # Before changing head, check for the cases like
        # empty list and list with only one node
        if temp is not None:
            self.head = temp.prev
 
    # Given a reference to the head of a list and an
    # integer,inserts a new node on the front of list
    def push(self, new_data):
 
        # 1. Allocates node
        # 2. Put the data in it
        new_node = Node(new_data)
 
        # 3. Make next of new node as head and
        # previous as None (already None)
        new_node.next = self.head
 
        # 4. change prev of head node to new_node
        if self.head is not None:
            self.head.prev = new_node
 
        # 5. move the head to point to the new node
        self.head = new_node
 
    def printList(self, node):
        while(node is not None):
            print(node.data, end=' ')
            node = node.next
 
 
# Driver's code
if __name__ == "__main__":
    dll = DoublyLinkedList()
    dll.push(2)
    dll.push(4)
    dll.push(8)
    dll.push(10)
 
    print("\nOriginal Linked List")
    dll.printList(dll.head)
 
    # Function call
    dll.reverse()
 
    print("\nReversed Linked List")
    dll.printList(dll.head)
 
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

Time complexity: linear
Space complexity: constant
# Python3 code for the above approach
 
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None
 
 
class DoublyLinkedList:
    def __init__(self):
        self.head = None
 
    """
    method to reverse a Doubly-Linked List using Stacks
    """
 
    def reverseUsingStacks(self):
 
        stack = []
        temp = self.head
        while temp is not None:
            stack.append(temp.data)
            temp = temp.next
 
        # Add all the elements in the stack
        # in a sequence to the stack
        temp = self.head
        while temp is not None:
            temp.data = stack.pop()
            temp = temp.next
 
        # Popped all the elements and the
        # added in the linked list,
        # in a reversed order.
 
    """
    method to push a new item before the head
    """
 
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
 
        if self.head is not None:
            self.head.prev = new_node
 
        self.head = new_node
 
    """
    method to traverse the doubly-linked
    list and print every node in the list
    """
 
    def printList(self, node):
        while(node is not None):
            print(node.data)
            node = node. next
 
 
# driver's code
if __name__ == "__main__":
    dll = DoublyLinkedList()
    dll.push(2)
    dll.push(4)
    dll.push(8)
    dll.push(10)
 
    print("original doubly-linked list")
    dll.printList(dll.head)
 
    # Function call
    dll.reverseUsingStacks()
 
    print(" reversed doubly-linked list")
    dll.printList(dll.head)

Time complexity: linear
Space complexity: linear
# Python3 program to find a pair with
# given sum x.
 
# Structure of node of doubly linked list
class Node:
     
    def __init__(self, x):
         
        self.data = x
        self.next = None
        self.prev = None
 
# Function to find pair whose sum
# equal to given value x.
def pairSum(head, x):
     
    # Set two pointers, first to the
    # beginning of DLL and second to
    # the end of DLL.
    first = head
    second = head
     
    while (second.next != None):
        second = second.next
 
    # To track if we find a pair or not
    found = False
 
    # The loop terminates when they
    # cross each other (second.next ==
    # first), or they become same
    # (first == second)
    while (first != second and second.next != first):
             
        # Pair found
        if ((first.data + second.data) == x):
            found = True
            print("(", first.data, ",",
                       second.data, ")")
             
            # Move first in forward direction
            first = first.next
             
            # Move second in backward direction
            second = second.prev
        else:
            if ((first.data + second.data) < x):
                first = first.next
            else:
                second = second.prev
 
    # If pair is not present
    if (found == False):
        print("No pair found")
 
# A utility function to insert a new node
# at the beginning of doubly linked list
def insert(head, data):
     
    temp = Node(data)
     
    if not head:
        head = temp
    else:
        temp.next = head
        head.prev = temp
        head = temp
         
    return head
 
# Driver code
if __name__ == '__main__':
     
    head = None
    head = insert(head, 9)
    head = insert(head, 8)
    head = insert(head, 6)
    head = insert(head, 5)
    head = insert(head, 4)
    head = insert(head, 2)
    head = insert(head, 1)
    x = 7
 
    pairSum(head, x)
 
# This code is contributed by mohit kumar 29

Time complexity: linear
Space complexity: constant
# Python3 implementation to insert
# value in sorted way in a sorted
# doubly linked list
 
# Node of a doubly linked list
class Node:
 
    # Constructor to initialize
    # the node object
    def __init__(self, data):
         
        self.data = data
        self.next = None
        self.prev = None
 
class DoublyLinkedList:
 
    # Function to initialize head
    def __init__(self):
         
        self.head = None
 
    # Function to create and return a
    # new node of a doubly linked list
    def getNode(self, data):
         
        return Node(data)
 
    # Function to insert a new node in
    # sorted way in a sorted doubly linked list
    def sortedInsert(self, data):
         
        new_node = self.getNode(data)
 
        # If the list is empty
        if self.head is None:
            self.head = new_node
         
        # If the node is to be inserted at 
        # the beginning of the doubly linked list
        elif self.head.data >= new_node.data:
            new_node.next = self.head
            new_node.next.prev = new_node
            self.head = new_node
        else:
            current = self.head
 
            # Locate the node after which
            # the new node  is to be inserted
            while ((current.next is not None) and
                   (current.next.data < new_node.data)):
                current = current.next
     
            # Make the appropriate links
            new_node.next = current.next
 
            # If the new node is not inserted
            # at the end of the list
            if current.next is not None:
                new_node.next.prev = new_node
 
            current.next = new_node
            new_node.prev = current
     
    # Function to print the doubly linked list
    def printList(self):
 
        node = self.head
        while node:
            print(str(node.data), end = " ")
            node = node.next
 
# Driver code
if __name__ == '__main__':
 
    # Insert the following nodes
    # in sorted way
    llist = DoublyLinkedList()
    llist.sortedInsert(8)
    llist.sortedInsert(5)
    llist.sortedInsert(3)
    llist.sortedInsert(10)
    llist.sortedInsert(12)
    llist.sortedInsert(9)
     
    print("Created Doubly Linked List")
    llist.printList()
 
# This code is contributed by Siddhartha Pramanik

Time complexity: linear
Space complexity: linear
# Python3 implementation to count triplets
# in a sorted doubly linked list
# whose sum is equal to a given value 'x'
  
# structure of node of doubly linked list
class Node: 
    def __init__(self):
        self.data = None
        self.prev = None
        self.next = None
  
# function to count triplets in a sorted doubly linked list
# whose sum is equal to a given value 'x'
def countTriplets( head, x):
  
    ptr1 = head
    ptr2 = None
    ptr3 = None
    count = 0
  
    # generate all possible triplets
    while (ptr1 != None ):
        ptr2 = ptr1.next
        while ( ptr2 != None ):
            ptr3 = ptr2.next
            while ( ptr3 != None ):
          
                # if elements in the current triplet sum up to 'x'
                if ((ptr1.data + ptr2.data + ptr3.data) == x):
  
                    # increment count
                    count = count + 1
                ptr3 = ptr3.next
            ptr2 = ptr2.next
        ptr1 = ptr1.next
  
    # required count of triplets
    return count
  
# A utility function to insert a new node at the
# beginning of doubly linked list
def insert(head, data):
  
    # allocate node
    temp = Node()
  
    # put in the data
    temp.data = data
    temp.next = temp.prev = None
  
    if ((head) == None):
        (head) = temp
    else :
        temp.next = head
        (head).prev = temp
        (head) = temp
    return head
      
# Driver code
  
# start with an empty doubly linked list
head = None
  
# insert values in sorted order
head = insert(head, 9)
head = insert(head, 8)
head = insert(head, 6)
head = insert(head, 5)
head = insert(head, 4)
head = insert(head, 2)
head = insert(head, 1)
  
x = 17
  
print( "Count = ", countTriplets(head, x))
  
# This code is contributed by Arnab Kundu

Time complexity: cubic
Space complexity: constant
# Python3 implementation to count triplets in a sorted doubly linked list
# whose sum is equal to a given value 'x'
   
# structure of node of doubly linked list
class Node:
      
    def __init__(self, data):
          
        self.data=data
        self.next=None
        self.prev=None
  
   
# function to count triplets in a sorted doubly linked list
# whose sum is equal to a given value 'x'
def countTriplets(head, x):
  
    ptr2=head
    count = 0;
   
    # unordered_map 'um' implemented as hash table
    um = dict()
      
    ptr = head
    # insert the <node data, node pointer> tuple in 'um'
    while ptr!=None:
        um[ptr.data] = ptr;
        ptr = ptr.next
   
    # generate all possible pairs
    ptr1=head
      
    while ptr1!=None:
          
        ptr2 = ptr1.next
          
        while ptr2!=None:
   
            # p_sum - sum of elements in the current pair
            p_sum = ptr1.data + ptr2.data;
   
            # if 'x-p_sum' is present in 'um' and either of the two nodes
            # are not equal to the 'um[x-p_sum]' node
            if ((x-p_sum) in um) and um[x - p_sum] != ptr1 and um[x - p_sum] != ptr2:
   
                # increment count
                count+=1
            ptr2 = ptr2.next
        ptr1 = ptr1.next
          
   
    # required count of triplets
    # division by 3 as each triplet is counted 3 times
    return (count // 3);
  
   
# A utility function to insert a new node at the
# beginning of doubly linked list
def insert(head, data):
  
    # allocate node
    temp = Node(data);
   
    if ((head) == None):
        (head) = temp;
    else:
        temp.next = head;
        (head).prev = temp;
        (head) = temp;
    return head
      
   
# Driver program to test above
if __name__=='__main__':
      
    # start with an empty doubly linked list
    head = None;
   
    # insert values in sorted order
    head = insert(head, 9);
    head = insert(head, 8);
    head = insert(head, 6);
    head = insert(head, 5);
    head = insert(head, 4);
    head = insert(head, 2);
    head = insert( head, 1);
   
    x = 17;
   
    print("Count = "+ str(countTriplets(head, x)))
      
# This code is contributed by rutvik_56

Time complexity: quadratic
Space complexity: linear
# Python3 implementation to count triplets
# in a sorted doubly linked list whose sum
# is equal to a given value 'x'
  
# Structure of node of doubly linked list
class Node:
      
    def __init__(self, x):
          
        self.data = x
        self.next = None
        self.prev = None
  
# Function to count pairs whose sum
# equal to given 'value'
def countPairs(first, second, value):
      
    count = 0
  
    # The loop terminates when either of two pointers
    # become None, or they cross each other (second.next
    # == first), or they become same (first == second)
    while (first != None and second != None and
           first != second and second.next != first):
  
        # Pair found
        if ((first.data + second.data) == value):
              
            # Increment count
            count += 1
  
            # Move first in forward direction
            first = first.next
  
            # Move second in backward direction
            second = second.prev
  
        # If sum is greater than 'value'
        # move second in backward direction
        elif ((first.data + second.data) > value):
            second = second.prev
  
        # Else move first in forward direction
        else:
            first = first.next
  
    # Required count of pairs
    return count
  
# Function to count triplets in a sorted 
# doubly linked list whose sum is equal 
# to a given value 'x'
def countTriplets(head, x):
      
    # If list is empty
    if (head == None):
        return 0
  
    current, first, last = head, None, None
    count = 0
  
    # Get pointer to the last node of
    # the doubly linked list
    last = head
      
    while (last.next != None):
        last = last.next
  
    # Traversing the doubly linked list
    while current != None:
  
        # For each current node
        first = current.next
  
        # count pairs with sum(x - current.data) in
        # the range first to last and add it to the
        # 'count' of triplets
        count, current = count + countPairs(
            first, last, x - current.data), current.next
  
    # Required count of triplets
    return count
  
# A utility function to insert a new node 
# at the beginning of doubly linked list
def insert(head, data):
      
    # Allocate node
    temp = Node(data)
  
    # Put in the data
    # temp.next = temp.prev = None
    if (head == None):
        head = temp
    else:
        temp.next = head
        head.prev = temp
        head = temp
          
    return head
  
# Driver code
if __name__ == '__main__':
      
    # Start with an empty doubly linked list
    head = None
  
    # Insert values in sorted order
    head = insert(head, 9)
    head = insert(head, 8)
    head = insert(head, 6)
    head = insert(head, 5)
    head = insert(head, 4)
    head = insert(head, 2)
    head = insert(head, 1)
  
    x = 17
  
    print("Count = ", countTriplets(head, x))
      
# This code is contributed by mohit kumar 29

Time complexity: quadratic
Space complexity: constant
# Python implementation to remove duplicates from a
# sorted doubly linked list
 
# A linked list node
class Node:
    def __init__(self, new_data):
        self.data = new_data
        self.next = None
        self.prev = None
 
# Function to _delete a node in a Doubly Linked List.
# head_ref -. pointer to head node pointer.
# _del -. pointer to node to be _deleted.
def _deleteNode(head_ref, _del):
 
    # base case
    if (head_ref == None or _del == None):
        return
 
    # If node to be _deleted is head node
    if (head_ref == _del):
        head_ref = _del.next
 
    # Change next only if node to be _deleted
    # is NOT the last node
    if (_del.next != None):
        _del.next.prev = _del.prev
 
    # Change prev only if node to be _deleted
    # is NOT the first node
    if (_del.prev != None):
        _del.prev.next = _del.next
 
    return head_ref
 
# function to remove duplicates from a
# sorted doubly linked list
def removeDuplicates(head_ref):
 
    # if list is empty
    if ((head_ref) == None):
        return None
 
    current = head_ref
    next = None
 
    # traverse the list till the last node
    while (current.next != None) :
 
        # Compare current node with next node
        if (current.data == current.next.data):
 
            # _delete the node pointed to by
            # 'current.next'
            _deleteNode(head_ref, current.next)
 
        # else simply move to the next node
        else:
            current = current.next
     
    return head_ref
     
# Function to insert a node at the beginning
# of the Doubly Linked List
def push(head_ref, new_data):
 
    # allocate node
    new_node = Node(0)
 
    # put in the data
    new_node.data = new_data
 
    # since we are adding at the beginning,
    # prev is always None
    new_node.prev = None
 
    # link the old list off the new node
    new_node.next = (head_ref)
 
    # change prev of head node to new node
    if ((head_ref) != None):
        (head_ref).prev = new_node
 
    # move the head to point to the new node
    (head_ref) = new_node
     
    return head_ref
 
# Function to print nodes in a given doubly linked list
def printList(head):
 
    # if list is empty
    if (head == None):
        print("Doubly Linked list empty")
 
    while (head != None) :
        print(head.data, end = " ")
        head = head.next
     
# Driver program to test above functions
 
# Start with the empty list
head = None
 
# Create the doubly linked list:
# 4<->4<->4<->4<->6<->8<->8<->10<->12<->12
head = push(head, 12)
head = push(head, 12)
head = push(head, 10)
head = push(head, 8)
head = push(head, 8)
head = push(head, 6)
head = push(head, 4)
head = push(head, 4)
head = push(head, 4)
head = push(head, 4)
 
print( "Original Doubly linked list:\n")
printList(head)
 
# remove duplicate nodes
head = removeDuplicates(head)
 
print("\nDoubly linked list after removing duplicates:")
printList(head)
 
# This code is contributed by Arnab Kundu

Time complexity: linear
Space complexity: constant
# Python implementation to sort a k sorted doublyclass DoublyLinkedList {
head = None
 
class Node:
    def __init__(self, val):
        self.data = val
        self.prev = None
        self.next = None
 
# function to sort a k sorted doubly linked list
# Using Insertion Sort
# Time Complexity: O(n*k)
# Space Complexity: O(1)
def sortAKSortedDLL(head , k):
    if (head == None or head.next == None):
        return head
 
    # perform on all the nodes in list
    i = head.next
    while(i != None):
        j = i
         
        # There will be atmost k swaps for each element in the list
        # since each node is k steps away from its correct position
        while (j.prev != None and j.data < j.prev.data):
           
            # swap j and j.prev node
            temp = j.prev.prev
            temp2 = j.prev
            temp3 = j.next
            j.prev.next = temp3
            j.prev.prev = j
            j.prev = temp
            j.next = temp2
            if (temp != None):
                temp.next = j
            if (temp3 != None):
                temp3.prev = temp2
                 
            # if j is now the new head
            # then reset head
        if (j.prev == None):
            head = j
        i = i.next
 
    return head
 
# UTILITY FUNCTIONS
#
# Function to insert a node at the beginning of the Doubly Linked List
def push(new_data):
 
    global head
     
    # allocate node
    new_node = Node(new_data)
 
    #
    # since we are adding at the beginning, prev is always NULL
    #
    new_node.prev = None
 
    # link the old list of the new node
    new_node.next = head
 
    # change prev of head node to new node
    if (head != None):
        head.prev = new_node
 
    # move the head to point to the new node
    head = new_node
 
# Function to print nodes in a given doubly linked list This function is same
# as printList() of singly linked list
def printList(node):
    while (node != None):
        print(node.data,end = " ")
        node = node.next
 
# Driver code
 
# Let us create a k sorted doubly linked list to test the functions Created
# doubly linked list will be 3<->6<->2<->12<->56<->8
push(8)
push(56)
push(12)
push(2)
push(6)
push(3)
 
k = 2
 
print("Original Doubly linked list:")
printList(head)
 
sortedDLL = sortAKSortedDLL(head, k)
print("")
print("Doubly Linked List after sorting:")
printList(sortedDLL)
 
# This codeis contributed by shinjanpatra

Time complexity: quadratic
Space complexity: constant