Banana-Leopard/TASTY huggingface dataset:
# Python implementation to sort a k sorted doubly linked list
import heapq
head = None
 
#  a node of the doubly linked list
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None
 
# function to sort a k sorted doubly linked list
def sortAKSortedDLL(head, k):
    # if list is empty
    if head == None:
        return head
 
    pq = []
 
    newHead = None
    last = None
 
    for i in range(k+1):
        # push the node
        heapq.heappush(pq, (head.data, head))
        # move to the next node
        head = head.next
 
    # loop till there are elements in 'pq'
    while len(pq) > 0:
 
        if newHead == None:
            newHead = heapq.heappop(pq)[1]
            newHead.prev = None
 
        #  'last' points to the last node of the result sorted list so far
            last = newHead
        else:
            last.next = heapq.heappop(pq)[1]
            last.next.prev = last
            last = last.next
 
        # if there are more nodes left in the input list
        if head != None:
            # push the node
            heapq.heappush(pq, (head.data, head))
 
            # move to the next node
            head = head.next
 
    # making 'next' of last node point to NULL
    last.next = None
 
    # new head of the required sorted DLL
    return newHead
 
# 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 off 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
def printList(head):
    # if list is empty
    if head is None:
        print("Doubly Linked list empty")
 
    while head is not None:
        print(head.data, end=" ")
        head = head.next
 
 
# Driver code
if __name__ == '__main__':
 
    # Create the doubly linked list:
    # 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("\nDoubly Linked List after sorting:")
    printList(sortedDLL)
 
# This code is contributed by Tapesh(tapeshdua420)

Time complexity: nlogn
Space complexity: constant
# Python program to insetail nodes in doubly
# linked list such that list remains in
# ascending order on printing from left
# to right
 
# Linked List node
class Node:
    def __init__(self, data):
        self.info = data
        self.next = None
        self.prev = None
 
head = None
tail = None
                 
# Function to insetail new node
def nodeInsetail( key) :
 
    global head
    global tail
     
    p = Node(0)
    p.info = key
    p.next = None
 
    # If first node to be insetailed in doubly
    # linked list
    if ((head) == None) :
        (head) = p
        (tail) = p
        (head).prev = None
        return
     
    # If node to be insetailed has value less
    # than first node
    if ((p.info) < ((head).info)) :
        p.prev = None
        (head).prev = p
        p.next = (head)
        (head) = p
        return
 
    # If node to be insetailed has value more
    # than last node
    if ((p.info) > ((tail).info)) :
     
        p.prev = (tail)
        (tail).next = p
        (tail) = p
        return
     
    # Find the node before which we need to
    # insert p.
    temp = (head).next
    while ((temp.info) < (p.info)) :
        temp = temp.next
 
    # Insert new node before temp
    (temp.prev).next = p
    p.prev = temp.prev
    temp.prev = p
    p.next = temp
 
# Function to print nodes in from left to right
def printList(temp) :
 
    while (temp != None) :
     
        print( temp.info, end = " ")
        temp = temp.next
     
# Driver program to test above functions
nodeInsetail( 30)
nodeInsetail( 50)
nodeInsetail( 90)
nodeInsetail( 10)
nodeInsetail( 40)
nodeInsetail( 110)
nodeInsetail( 60)
nodeInsetail( 95)
nodeInsetail( 23)
 
print("Doubly linked list on printing from left to right\n" )
 
printList(head)
 
# This code is contributed by Arnab Kundu

Time complexity: linear
Space complexity: constant
# Python code to rotate doubly linked list by N nodes.
class Node:
    def __init__(self, data):
        self.data = data
        self.pre = None
        self.next = None
 
class GFG:
    def __init__(self):
        self.head = None
 
    # Function to insert nodes at the start of the linked list.
    def insertAtHead(self, data):
        n = Node(data)
        if self.head == None:
            self.head = n
            return
 
        n.next = self.head
        self.head.pre = n
        self.head = n
        return
 
    # Function to insert nodes at the tail of the linked list.
    def insertAtTail(self, data):
        if self.head == None:
            self.insertAtHead(data)
            return
        temp = self.head
        while temp.next != None:
            temp = temp.next
        n = Node(data)
        temp.next = n
        n.pre = temp
        return
       
    # Function to print the list.
    def display(self):
        temp = self.head
        while temp != None:
            print(temp.data, "-->", sep="", end="")
            temp = temp.next
        print("NULL")
 
    # Function to rotate doubly linked list by N nodes.
    def rotateByN(self, pos):
        if pos == 0:
            return
        curr = self.head
        while pos:
            curr = curr.next
            pos -= 1
        tail = curr.pre
        NewHead = curr
        tail.next = None
        curr.pre = None
 
        while curr.next != None:
            curr = curr.next
 
        curr.next = self.head
        self.head.pre = curr
        self.head = NewHead
 
 
# Driver Code
if __name__ == "__main__":
    list = GFG()
    list.insertAtTail('a')
    list.insertAtTail('b')
    list.insertAtTail('c')
    list.insertAtTail('d')
    list.insertAtTail('e')
 
    n = 2
    print("Before Rotation : ")
    list.display()
    list.rotateByN(n)
    print("\nAfter Rotation : ")
    list.display()
    print()
 
# This code is contributed by Tapesh(tapeshdua420)

Time complexity: linear
Space complexity: constant
class Node:
    def __init__(self):
        self.data = 0;
        self.next = None;
        self.next = None;
 
# Function to add Node at the end of a
# Doubly LinkedList
def insertAtEnd(head, data):
    new_Node = Node();
    new_Node.data = data;
    new_Node.next = None;
    temp = head;
 
    if (head == None):
        new_Node.prev = None;
        head = new_Node;
        return head;
     
 
    while (temp.next != None):
        temp = temp.next;
     
    temp.next = new_Node;
    new_Node.prev = temp;
    return head;
 
 
# Function to print Doubly LinkedList
def printDLL(head):
    while (head != None):
        print(head.data, end=" ");
        head = head.next;
     
    print();
 
 
# Function to Reverse a doubly linked list
# in groups of given size
def reverseByN(head, k):
    if (head == None):
        return None;
 
    head.prev = None;
    temp=None;
    curr = head;
    newHead = None;
    count = 0;
 
    while (curr != None and count < k):
        newHead = curr;
        temp = curr.prev;
        curr.prev = curr.next;
        curr.next = temp;
        curr = curr.prev;
        count += 1;
     
    # Checking if the reversed LinkedList size is
    # equal to K or not. If it is not equal to k
    # that means we have reversed the last set of
    # size K and we don't need to call the
    # recursive function
    if (count >= k):
        rest = reverseByN(curr, k);
        head.next = rest;
        if (rest != None):
           
            # it is required for prev link otherwise u
            # wont be backtrack list due to broken
            # links
            rest.prev = head;
     
    return newHead;
 
 
# Driver code
if __name__ == '__main__':
    head = None;
    for i in range(1,11):
        head = insertAtEnd(head, i);
     
    printDLL(head);
    n = 4;
 
    head = reverseByN(head, n);
    printDLL(head);
 
# This code contributed by umadevi9616

Time complexity: linear
Space complexity: constant
# Python implementation to reverse a doubly
# linked list in groups of given size without recursion
# Iterative method.
 
# Represents a node of doubly linked list.
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
 
# Function to get a new Node.
def getNode(data):
    # allocating node
    new_node = Node(0)
    new_node.data = data
    new_node.next = new_node.prev = None
    return new_node
 
# Function to insert a node at the beginning of the doubly linked list.
 
 
def push(head, new_node):
    # since we are adding at the beginning, prev is always null.
    new_node.prev = None
    # link the old list off 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
    return head
 
# Function to print nodes in given doubly linked list.
 
 
def printList(head):
    while (head):
        print(head.data, end=" ")
        head = head.next
 
# Function to reverse a doubly linked list in groups of given size.
 
 
def revListInGroupOfGivenSize(head, k):
    if head is None:
        return head
    st = head
    globprev, ans = None, None
    while (st != None):
        # Count the number of nodes.
        count = 1
        curr = st
        prev, next_node = None, None
        while (curr != None and count <= k):
            # Reversing k nodes.
            next_node = curr.next
            curr.prev = next_node
            curr.next = prev
            prev = curr
            curr = next_node
            count += 1
 
        if ans is None:
            ans = prev
            ans.prev = None
 
        if globprev is None:
            globprev = st
 
        else:
            globprev.next = prev
            prev.prev = globprev
            globprev = st
 
        st = curr
 
    return ans
 
 
# Start with the empty list.
head = None
 
# Create a doubly linked list: 10<->8<->4<->2
head = push(head, getNode(2))
head = push(head, getNode(4))
head = push(head, getNode(8))
head = push(head, getNode(10))
 
print("Original list:", end=" ")
printList(head)
 
k = 2
 
# Reverse doubly linked list in groups of size 'k'
head = revListInGroupOfGivenSize(head, k)
 
print("\nModified list:", end=" ")
printList(head)
 
# This code is contributed by lokesh (lokeshmvs21).

Time complexity: linear
Space complexity: constant
# Python3 program to implement Stack
# using linked list so that reverse
# can be done with O(1) extra space.
class StackNode:
     
    def __init__(self, data):
         
        self.data = data
        self.next = None
 
class Stack:
     
    def __init__(self):
          
        self.top = None
      
    # Push and pop operations
    def push(self, data):
     
        if (self.top == None):
            self.top = StackNode(data)
            return
         
        s = StackNode(data)
        s.next = self.top
        self.top = s
      
    def pop(self):
     
        s = self.top
        self.top = self.top.next
        return s
  
    # Prints contents of stack
    def display(self):
     
        s = self.top
         
        while (s != None):
            print(s.data, end = ' ')
            s = s.next
         
    # Reverses the stack using simple
    # linked list reversal logic.
    def reverse(self):
 
        prev = self.top
        cur = self.top
        cur = cur.next
        succ = None
        prev.next = None
         
        while (cur != None):
            succ = cur.next
            cur.next = prev
            prev = cur
            cur = succ
         
        self.top = prev
     
# Driver code
if __name__=='__main__':
     
    s = Stack()
    s.push(1)
    s.push(2)
    s.push(3)
    s.push(4)
     
    print("Original Stack")
    s.display()
    print()
      
    # Reverse
    s.reverse()
  
    print("Reversed Stack")
    s.display()
      
# This code is contributed by rutvik_56

Time complexity: linear
Space complexity: constant
# Python3 program to find a list in second list
class Node:
    def __init__(self, value = 0):
         
        self.value = value
        self.next = None
 
# Returns true if first list is
# present in second list
def findList(first, second):
     
    # If both linked lists are empty/None,
    # return True
    if not first and not second:
        return True
 
    # If ONLY one of them is empty,
    # return False
    if not first or not second:
        return False
 
    ptr1 = first
    ptr2 = second
 
    # Traverse the second LL by
    # picking nodes one by one
    while ptr2:
 
        # Initialize 'ptr2' with current
        # node of 'second'
        ptr2 = second
 
        # Start matching first LL
        # with second LL
        while ptr1:
 
            # If second LL become empty and
            # first not, return False,
            # since first LL has not been
            # traversed completely
            if not ptr2:
                return False
 
            # If value of both nodes from both
            # LLs are equal, increment pointers
            # for both LLs so that next value
            # can be matched
            else if ptr1.value == ptr2.value:
                ptr1 = ptr1.next
                ptr2 = ptr2.next
 
            # If a single mismatch is found
            # OR ptr1 is None/empty,break out
            # of the while loop and do some checks
            else:
                break
 
        # check 1 :
        # If 'ptr1' is None/empty,that means
        # the 'first LL' has been completely
        # traversed and matched so return True
        if not ptr1:
            return True
 
        # If check 1 fails, that means, some
        # items for 'first' LL are still yet
        # to be matched, so start again by
        # bringing back the 'ptr1' to point
        # to 1st node of 'first' LL
        ptr1 = first
         
        # And increment second node element to next
        second = second.next
         
    return False
 
# Driver Code
 
# Let us create two linked lists to
# test the above functions.
# Created lists would be be
# node_a: 1->2->3->4
# node_b: 1->2->1->2->3->4
node_a = Node(1)
node_a.next = Node(2)
node_a.next.next = Node(3)
node_a.next.next.next = Node(4)
 
node_b = Node(1)
node_b.next = Node(2)
node_b.next.next = Node(1)
node_b.next.next.next = Node(2)
node_b.next.next.next.next = Node(3)
node_b.next.next.next.next.next = Node(4)
 
if findList(node_a, node_b):
    print("LIST FOUND")
else:
    print("LIST NOT FOUND")
 
# This code is contributed by GauriShankarBadola

Time complexity: quadratic
Space complexity: constant
# Python3 program to implement unrolled
# linked list and traversing it.
maxElements = 4
 
# Unrolled Linked List Node
class Node:
     
    def __init__(self):
         
        self.numElements = 0
        self.array = [0 for i in range(maxElements)]
        self.next = None
 
# Function to traverse an unrolled linked list
# and print all the elements
def printUnrolledList(n):
 
    while (n != None):
 
        # Print elements in current node
        for i in range(n.numElements):
            print(n.array[i], end = ' ')
 
        # Move to next node
        n = n.next
 
# Driver Code
if __name__=='__main__':
     
    head = None
    second = None
    third = None
 
    # Allocate 3 Nodes
    head = Node()
    second = Node()
    third = Node()
 
    # Let us put some values in second
    # node (Number of values must be
    # less than or equal to
    # maxElement)
    head.numElements = 3
    head.array[0] = 1
    head.array[1] = 2
    head.array[2] = 3
 
    # Link first Node with the second Node
    head.next = second
 
    # Let us put some values in second node
    # (Number of values must be less than
    # or equal to maxElement)
    second.numElements = 3
    second.array[0] = 4
    second.array[1] = 5
    second.array[2] = 6
 
    # Link second Node with the third Node
    second.next = third
 
    # Let us put some values in third node
    # (Number of values must be less than
    # or equal to maxElement)
    third.numElements = 3
    third.array[0] = 7
    third.array[1] = 8
    third.array[2] = 9
    third.next = None
 
    printUnrolledList(head)
     
# This code is contributed by rutvik_56

Time complexity: linear
Space complexity: linear
# Python3 program to find fractional node
# in a linked list
import math
 
# Linked list node
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
# Function to create a new node
# with given data
def newNode(data):
    new_node = Node(data)
    new_node.data = data
    new_node.next = None
    return new_node
 
# Function to find fractional node
# in the linked list
def fractionalNodes(head, k):
     
    # Corner cases
    if (k <= 0 or head == None):
        return None
 
    fractionalNode = None
 
    # Traverse the given list
    i = 0
    temp = head
    while (temp != None):
         
        # For every k nodes, we move
        # fractionalNode one step ahead.
        if (i % k == 0):
 
            # First time we see a multiple of k
            if (fractionalNode == None):
                fractionalNode = head
 
            else:
                fractionalNode = fractionalNode.next
 
        i = i + 1
        temp = temp.next
 
    return fractionalNode
 
# A utility function to print a linked list
def printList(node):
    while (node != None):
        print(node.data, end = ' ')
        node = node.next
 
# Driver Code
if __name__ == '__main__':
    head = newNode(1)
    head.next = newNode(2)
    head.next.next = newNode(3)
    head.next.next.next = newNode(4)
    head.next.next.next.next = newNode(5)
    k = 2
 
    print("List is", end = ' ')
    printList(head)
 
    answer = fractionalNodes(head, k)
    print("\nFractional node is", end = ' ')
    print(answer.data)
 
# This code is contributed by Srathore

Time complexity: linear
Space complexity: constant
# Python3 program to find modular node
# in a linked list
import math
 
# Linked list node
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
# Function to create a new node
# with given data
def newNode(data):
    new_node = Node(data)
    new_node.data = data
    new_node.next = None
    return new_node
 
# Function to find modular node
# in the linked list
def modularNode(head, k):
     
    # Corner cases
    if (k <= 0 or head == None):
        return None
 
    # Traverse the given list
    i = 1
    modularNode = None
    temp = head
    while (temp != None):
        if (i % k == 0):
            modularNode = temp
 
        i = i + 1
        temp = temp.next
    return modularNode
 
# Driver Code
if __name__ == '__main__':
    head = newNode(1)
    head.next = newNode(2)
    head.next.next = newNode(3)
    head.next.next.next = newNode(4)
    head.next.next.next.next = newNode(5)
    k = 2
    answer = modularNode(head, k)
    print("Modular node is", end = ' ')
    if (answer != None):
        print(answer.data, end = ' ')
    else:
        print("None")
 
# This code is contributed by Srathore

Time complexity: linear
Space complexity: constant
# Python3 program to find smallest and largest
# elements in singly linked list.
 
# Linked list node
class Node:
 
    def __init__(self):
        self.data = None
        self.next = None
 
head = None
 
# Function that returns the largest element
# from the linked list.
def largestElement(head):
 
    # Declare a max variable and initialize
    # it with INT_MIN value.
    # INT_MIN is integer type and its value
    # is -32767 or less.
    max = -32767
 
    # Check loop while head not equal to None
    while (head != None):
     
        # If max is less than head.data then
        # assign value of head.data to max
        # otherwise node point to next node.
        if (max < head.data) :
            max = head.data
        head = head.next
     
    return max
 
# Function that returns smallest element
# from the linked list.
def smallestElement(head):
 
    # Declare a min variable and initialize
    # it with INT_MAX value.
    # INT_MAX is integer type and its value
    # is 32767 or greater.
    min = 32767
 
    # Check loop while head not equal to None
    while (head != None) :
     
        # If min is greater than head.data then
        # assign value of head.data to min
        # otherwise node point to next node.
        if (min > head.data) :
            min = head.data
        head = head.next
     
    return min
 
# Function that push the element in linked list.
def push( data) :
 
    global head
 
    # Allocate dynamic memory for newNode.
    newNode = Node()
 
    # Assign the data into newNode.
    newNode.data = data
 
    # newNode.next assign the address of
    # head node.
    newNode.next = (head)
 
    # newNode become the headNode.
    (head) = newNode
 
# Display linked list.
def printList( head) :
 
    while (head != None) :
        print(head.data ,end= " -> ")
        head = head.next
     
    print("None")
 
# Driver code
 
# Start with empty list
# head = new Node()
 
# Using push() function to construct
# singly linked list
# 17.22.13.14.15
push( 15)
push( 14)
push( 13)
push( 22)
push( 17)
print("Linked list is : ")
 
# Call printList() function to
# display the linked list.
printList(head)
print("Maximum element in linked list: ",end="")
 
# Call largestElement() function to get
# largest element in linked list.
print(largestElement(head))
print("Minimum element in linked list: ",end="")
 
# Call smallestElement() function to get
# smallest element in linked list.
print(smallestElement(head),end="")
 
# This code is contributed by Arnab Kundu

Time complexity: linear
Space complexity: constant