Banana-Leopard/TASTY huggingface dataset:
# Node class
class Node:
    
    # Function to initialise the node object
    def __init__(self, data):
        self.data = data  # Assign data
        self.next = None  # Initialize next as null
    
# Linked List class contains a Node object
class LinkedList:
    
    # Function to initialize head
    def __init__(self):
        self.head = None
  
    # Function to insert a new node at the beginning 
    def push(self, new_data): 
        new_node = Node(new_data) 
        new_node.next = self.head 
        self.head = new_node
  
    # Print the linked list
    def printList(self):
        node = self.head
        while node:
            print(str(node.data) + "->", end = "")
            node = node.next
        print("NULL")
  
    # Function to get the middle of
    #  the linked list
    def printMiddle(self):
        count = 0
        mid = self.head
        heads = self.head
   
        while(heads != None):
       
        # Update mid, when 'count'
        # is odd number
            if count&1:
                mid = mid.next
            count += 1
            heads = heads.next
             
        # If empty list is provided
        if mid!=None:
            print("The middle element is ", mid.data)
  
# Code execution starts here
if __name__=='__main__':
    
    # Start with the empty list
    llist = LinkedList()
    
    for i in range(5, 0, -1):
        llist.push(i)
        llist.printList()
        llist.printMiddle()
  
 # This Code is contributed by Manisha_Ediga

Time complexity: linear
Space complexity: constant
# Python3 program to check if linked
# list is palindrome using stack
 
 
class Node:
    def __init__(self, data):
 
        self.data = data
        self.ptr = None
 
# Function to check if the linked list
# is palindrome or not
 
 
def ispalindrome(head):
 
    # Temp pointer
    slow = head
 
    # Declare a stack
    stack = []
 
    ispalin = True
 
    # Push all elements of the list
    # to the stack
    while slow != None:
        stack.append(slow.data)
 
        # Move ahead
        slow = slow.ptr
 
    # Iterate in the list again and
    # check by popping from the stack
    while head != None:
 
        # Get the top most element
        i = stack.pop()
 
        # Check if data is not
        # same as popped element
        if head.data == i:
            ispalin = True
        else:
            ispalin = False
            break
 
        # Move ahead
        head = head.ptr
 
    return ispalin
 
# Driver Code
 
 
# Addition of linked list
one = Node(1)
two = Node(2)
three = Node(3)
four = Node(4)
five = Node(3)
six = Node(2)
seven = Node(1)
 
# Initialize the next pointer
# of every current pointer
one.ptr = two
two.ptr = three
three.ptr = four
four.ptr = five
five.ptr = six
six.ptr = seven
seven.ptr = None
 
# Call function to check palindrome or not
result = ispalindrome(one)
 
print("isPalindrome:", result)
 
# This code is contributed by Nishtha Goel

Time complexity: linear
Space complexity: linear
# Python program for the above approach
 
# Head of the list
head = None
left = None
 
 
class Node:
    def __init__(self, val):
        self.data = val
        self.next = None
 
# Initial parameters to this function are
# &head and head
 
 
def isPalindromeUtil(right):
    global head, left
 
    left = head
 
    # Stop recursion when right becomes null
    if (right == None):
        return True
 
    # If sub-list is not palindrome then no need to
    # check for the current left and right, return
    # false
    isp = isPalindromeUtil(right.next)
    if (isp == False):
        return False
 
    # Check values at current left and right
    isp1 = (right.data == left.data)
 
    left = left.next
 
    # Move left to next node;
    return isp1
 
# A wrapper over isPalindrome(Node head)
 
 
def isPalindrome(head):
    result = isPalindromeUtil(head)
    return result
 
# Push a node to linked list. Note that
# this function changes the head
 
 
def push(new_data):
    global head
 
    # Allocate the node and put in the data
    new_node = Node(new_data)
 
    # Link the old list off the the new one
    new_node.next = head
 
    # Move the head to point to new node
    head = new_node
 
# A utility function to print a
# given linked list
 
 
def printList(ptr):
    while (ptr != None):
        print(ptr.data, end="->")
        ptr = ptr.next
 
    print("Null ")
 
 
# Driver Code
str = ['a', 'b', 'a', 'c', 'a', 'b', 'a']
 
for i in range(0, 7):
    push(str[i])
 
if (isPalindrome(head) and i != 0):
    print("Is Palindrome\n")
else:
    print("Not Palindrome\n")
 
# This code is contributed by saurabh_jaiswal.

Time complexity: linear
Space complexity: linear
# Python3 code to move the last item to front
 
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
 
class LinkedList:
    def __init__(self):
        self.head = None
 
    # Function to add a node
    # at the beginning of Linked List
    def push(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node
 
    # Function to print nodes in a
    # given linked list
    def printList(self):
        tmp = self.head
        while tmp is not None:
            print(tmp.data, end=", ")
            tmp = tmp.next
        print()
 
    # Function to bring the last node to the front
    def moveToFront(self):
        tmp = self.head
        sec_last = None  # To maintain the track of
        # the second last node
 
    # To check whether we have not received
    # the empty list or list with a single node
        if not tmp or not tmp.next:
            return
 
        # Iterate till the end to get
        # the last and second last node
        while tmp and tmp.next:
            sec_last = tmp
            tmp = tmp.next
 
        # point the next of the second
        # last node to None
        sec_last.next = None
 
        # Make the last node as the first Node
        tmp.next = self.head
        self.head = tmp
 
 
# Driver's Code
if __name__ == '__main__':
    llist = LinkedList()
 
    # swap the 2 nodes
    llist.push(5)
    llist.push(4)
    llist.push(3)
    llist.push(2)
    llist.push(1)
    print("Linked List before moving last to front ")
    llist.printList()
     
    # Function call
    llist.moveToFront()
    print("Linked List after moving last to front ")
    llist.printList()

Time complexity: linear
Space complexity: constant
''' Link list node '''
class Node:
    def __init__(self):  
        self.data = 0
        self.next = None
     
'''This solution uses the temporary
 dummy to build up the result list '''
def sortedIntersect(a, b):
    dummy = Node()
    tail = dummy;
    dummy.next = None;
  
    ''' Once one or the other
    list runs out -- we're done '''
    while (a != None and b != None):
        if (a.data == b.data):
            tail.next = push((tail.next), a.data);
            tail = tail.next;
            a = a.next;
            b = b.next;
         
        # advance the smaller list
        elif(a.data < b.data):
            a = a.next;
        else:
            b = b.next;   
    return (dummy.next);
 
''' UTILITY FUNCTIONS '''
''' Function to insert a node at
the beginning of the linked list '''
def push(head_ref, new_data):
 
    ''' allocate node '''
    new_node = Node()
  
    ''' put in the data  '''
    new_node.data = new_data;
  
    ''' link the old list off the new node '''
    new_node.next = (head_ref);
  
    ''' move the head to point to the new node '''
    (head_ref) = new_node;   
    return head_ref
 
''' Function to print nodes in
   a given linked list '''
def printList(node):
    while (node != None):
        print(node.data, end=' ')
        node = node.next;
      
''' Driver code'''
if __name__=='__main__':
     
    ''' Start with the empty lists '''
    a = None;
    b = None;
    intersect = None;
  
    ''' Let us create the first sorted
     linked list to test the functions
     Created linked list will be
     1.2.3.4.5.6 '''
    a = push(a, 6);
    a = push(a, 5);
    a = push(a, 4);
    a = push(a, 3);
    a = push(a, 2);
    a = push(a, 1);
  
    ''' Let us create the second sorted linked list
   Created linked list will be 2.4.6.8 '''
    b = push(b, 8);
    b = push(b, 6);
    b = push(b, 4);
    b = push(b, 2);
  
    ''' Find the intersection two linked lists '''
    intersect = sortedIntersect(a, b);
  
    print("Linked list containing common items of a & b ");
    printList(intersect);
 
# This code is contributed by rutvik_56.

Time complexity: linear
Space complexity: linear
# Python3 program to implement above approach
 
# Link list node
class Node:
    def __init__(self, d):
        self.data = d
        self.next = None
 
 
def sortedIntersect(a, b):
 
    result = Node(0)
    curr = result
 
    # Advance comparing the first
    # nodes in both lists.
    # When one or the other list runs
    # out, we're done.
    while (a != None and b != None):
            if (a.data == b.data):
                # found a node for the intersection
                curr.next = Node(a.data)
                curr = curr.next
 
                a = a.next
                b = b.next
            elif (a.data < b.data):
                a = a.next  # advance the smaller list
            else:
                b = b.next
    result = result.next
    return result
 
 
# UTILITY FUNCTIONS
# Function to insert a node at the beginning of the linked list
def push(head_ref, new_data):
 
    # Allocate node
    new_node = Node(new_data)
 
    # Link the old list of the new node
    new_node.next = head_ref
 
    # Move the head to point to the new node
    head_ref = new_node
    return head_ref
 
    # Function to print nodes in a given linked list
 
 
def printList(node):
    while (node != None):
        print(node.data, end=" ")
        node = node.next
 
 
# Driver code
 
# Start with the empty lists
a = None
b = None
intersect = None
 
# Let us create the first sorted linked list to test the functions Created
# linked list will be 1.2.3.4.5.6
 
a = push(a, 6)
a = push(a, 5)
a = push(a, 4)
a = push(a, 3)
a = push(a, 2)
a = push(a, 1)
 
# Let us create the second sorted linked list Created linked list will be
# 2.4.6.8
 
b = push(b, 8)
b = push(b, 6)
b = push(b, 4)
b = push(b, 2)
 
# Find the intersection two linked lists
intersect = sortedIntersect(a, b)
 
print("Linked list containing " + "common items of a & b")
printList(intersect)
 
# This code is contributed by Abhijeet Kumar(abhijeet19403)

Time complexity: linear
Space complexity: linear
# Link list node
class Node:
    def __init__(self):
        self.data = 0
        self.next = None
         
 
def sortedIntersect(a, b):
 
    # base case
    if (a == None or b == None):
        return None
 
    # If both lists are non-empty
    # Advance the smaller list and call recursively
    if (a.data < b.data):
        return sortedIntersect(a.next, b);
 
    if (a.data > b.data):
        return sortedIntersect(a, b.next);
 
    # Below lines are executed only
    # when a.data == b.data
    temp = Node();
    temp.data = a.data;
 
    # Advance both lists and call recursively
    temp.next = sortedIntersect(a.next, b.next);
    return temp;    
 
    # UTILITY FUNCTIONS
    # Function to insert a node at the beginning of the linked list
 
def push(head_ref, new_data):
 
    # Allocate node
    new_node = Node()
 
    # Put in the data
    new_node.data = new_data;
 
    # Link the old list of the new node
    new_node.next = head_ref;
 
    # Move the head to point to the new node
    head_ref = new_node;
    return head_ref;    
 
    # Function to print nodes in a given linked list
         
def printList(node):
    while (node != None):
        print(node.data, end=" ")
        node = node.next;        
 
 
# Driver code
 
# Start with the empty lists
a = None
b = None
intersect = None
 
# Let us create the first sorted linked list to test the functions Created
# linked list will be 1.2.3.4.5.6
         
a = push(a, 6)
a = push(a, 5)
a = push(a, 4)
a = push(a, 3)
a = push(a, 2)
a = push(a, 1)
 
# Let us create the second sorted linked list Created linked list will be
# 2.4.6.8
         
b = push(b, 8)
b = push(b, 6)
b = push(b, 4)
b = push(b, 2)
 
# Find the intersection two linked lists
intersect = sortedIntersect(a, b)
 
print("\n Linked list containing " + "common items of a & b");
printList(intersect)
 
# This code is contributed by Saurabh Jaiswal

Time complexity: linear
Space complexity: linear
# Python3 program to implement above approach
 
# Link list node
class Node:
    def __init__(self):
        self.data = 0
        self.next = None
 
 
def printList(node):
    while (node != None):
        print(node.data, end=" ")
        node = node.next;
 
 
def append(head_ref, new_data):
 
    new_node = Node()
 
    new_node.data = new_data;
 
    new_node.next = head_ref;
 
    head_ref = new_node;
    return head_ref;
 
 
def intersection(tmp1,tmp2,k):
    res = [0]*k
 
    set1 = set()
    while (tmp1 != None):
        set1.add(tmp1.data)
        tmp1 = tmp1.next
 
    cnt = 0
    while (tmp2 != None):
        if tmp2.data in set1:
            res[cnt] = tmp2.data;
            cnt += 1
       
        tmp2 = tmp2.next
    
    return res
   
def printList(node):
    while (node != None):
        print(node.data, end=" ")
        node = node.next;        
 
 
# Driver code
 
# Start with the empty lists
ll = None
ll1 = None
         
ll = append(ll , 7)
ll = append(ll , 6)
ll = append(ll , 5)
ll = append(ll , 4)
ll = append(ll , 3)
ll = append(ll , 2)
ll = append(ll , 1)
ll = append(ll , 0)
         
ll1 = append(ll1 , 7)
ll1 = append(ll1 , 6)
ll1 = append(ll1 , 5)
ll1 = append(ll1 , 4)
ll1 = append(ll1 , 3)
ll1 = append(ll1 , 12)
ll1 = append(ll1 , 0)
ll1 = append(ll1 , 9)
 
 
arr = intersection(ll , ll1 , 6)
 
for i in range(6):
  print(arr[i])
 
 
# This code is contributed by Abhijeet Kumar(abhijeet19403)

Time complexity: linear
Space complexity: linear
'''
 
sort a linked list using quick sort
 
'''
 
 
class Node:
    def __init__(self, val):
        self.data = val
        self.next = None
 
 
class QuickSortLinkedList:
 
    def __init__(self):
        self.head = None
 
    def addNode(self, data):
        if (self.head == None):
            self.head = Node(data)
            return
 
        curr = self.head
        while (curr.next != None):
            curr = curr.next
 
        newNode = Node(data)
        curr.next = newNode
 
    def printList(self, n):
        while (n != None):
            print(n.data, end=" ")
            n = n.next
 
    ''' takes first and last node,but do not
    break any links in    the whole linked list'''
 
    def paritionLast(self, start, end):
        if (start == end or start == None or end == None):
            return start
 
        pivot_prev = start
        curr = start
        pivot = end.data
 
        '''iterate till one before the end,
        no need to iterate till the end because end is pivot'''
 
        while (start != end):
            if (start.data < pivot):
 
                # keep tracks of last modified item
                pivot_prev = curr
                temp = curr.data
                curr.data = start.data
                start.data = temp
                curr = curr.next
            start = start.next
 
        '''swap the position of curr i.e.
        next suitable index and pivot'''
 
        temp = curr.data
        curr.data = pivot
        end.data = temp
 
        ''' return one previous to current because
        current is now pointing to pivot '''
        return pivot_prev
 
    def sort(self, start, end):
        if(start == None or start == end or start == end.next):
            return
 
        # split list and partition recurse
        pivot_prev = self.paritionLast(start, end)
        self.sort(start, pivot_prev)
 
        '''
        if pivot is picked and moved to the start,
        that means start and pivot is same
        so pick from next of pivot
        '''
        if(pivot_prev != None and pivot_prev == start):
            self.sort(pivot_prev.next, end)
 
        # if pivot is in between of the list,start from next of pivot,
        # since we have pivot_prev, so we move two nodes
        elif (pivot_prev != None and pivot_prev.next != None):
            self.sort(pivot_prev.next.next, end)
 
 
if __name__ == "__main__":
    ll = QuickSortLinkedList()
    ll.addNode(30)
    ll.addNode(3)
    ll.addNode(4)
    ll.addNode(20)
    ll.addNode(5)
 
    N = ll.head
    while (N.next != None):
        N = N.next
 
    print("\nLinked List before sorting")
    ll.printList(ll.head)
 
    # Function call
    ll.sort(ll.head, N)
 
    print("\nLinked List after sorting")
    ll.printList(ll.head)
 
    # This code is contributed by humpheykibet.

Time complexity: nlogn
Space complexity: linear
# Python3 program to check if a linked list is circular
 
# Node class
 
 
class Node:
 
    # Function to initialise the node object
    def __init__(self, data):
        self.data = data  # Assign data
        self.next = None  # Initialize next as null
 
 
# Linked List class contains a Node object
class LinkedList:
 
    # Function to initialize head
    def __init__(self):
        self.head = None
 
 
def Circular(head):
    if head == None:
        return True
 
    # Next of head
    node = head.next
    i = 0
 
    # This loop would stop in both cases (1) If
    # Circular (2) Not circular
    while((node is not None) and (node is not head)):
        i = i + 1
        node = node.next
 
    return(node == head)
 
 
# Driver's code
if __name__ == '__main__':
    llist = LinkedList()
    llist.head = Node(1)
    second = Node(2)
    third = Node(3)
    fourth = Node(4)
 
    llist.head.next = second
    second.next = third
    third.next = fourth
 
    if (Circular(llist.head)):
        print('Yes')
    else:
        print('No')
 
    fourth.next = llist.head
 
    if (Circular(llist.head)):
        print('Yes')
    else:
        print('No')
 
# This code is contributed by Sanket Badhe

Time complexity: linear
Space complexity: constant
# Python3 program to find last man standing
  
# /* structure for a node in circular
#    linked list */
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None
  
# /* Function to find the only person left
#    after one in every m-th node is killed
#    in a circle of n nodes */
def getJosephusPosition(m, n):
    
    # Create a circular linked list of
    # size N.
    head = Node(1)
    prev = head
    for i in range(2, n + 1):
        prev.next = Node(i)
        prev = prev.next
    prev.next = head # Connect last
                       #node to first
  
    #/* while only one node is left in the
    #linked list*/
    ptr1 = head
    ptr2 = head
    while (ptr1.next != ptr1):
        # Find m-th node
        count = 1
        while (count != m):
            ptr2 = ptr1
            ptr1 = ptr1.next
            count += 1
  
        # /* Remove the m-th node */
        ptr2.next = ptr1.next
        # free(ptr1)
        ptr1 = ptr2.next
  
    print("Last person left standing (Josephus Position) is ", ptr1.data)
  
# /* Driver program to test above functions */
if __name__ == '__main__':
    n = 14
    m = 2
    getJosephusPosition(m, n)
  
# This code is contributed by mohit kumar 29

Time complexity: quadratic
Space complexity: linear
# Python3 program for converting 
# singly linked list into 
# circular linked list. 
import sys
  
# Linked list node 
class Node:
    def __init__(self,data):
        self.data = data
        self.next = None
  
def push(head, data):
    if not head:
        return Node(data)
  
    # Allocate dynamic memory 
    # for newNode. 
    # Assign the data into newNode. 
    newNode = Node(data)
  
    # newNode.next assign the 
    # address of head node. 
    newNode.next = head
  
    # newNode become the headNode. 
    head = newNode
    return head
  
# Function that convert 
# singly linked list into 
# circular linked list. 
def circular(head):
  
    # declare a node variable 
    # start and assign head 
    # node into start node. 
    start = head
  
    # check that while head.next 
    # not equal to null then head 
    # points to next node. 
    while(head.next is not None):
        head = head.next
  
    # if head.next points to null 
    # then start assign to the 
    # head.next node.     
    head.next = start
    return start
  
# Function that display the elements 
# of circular linked list. 
def displayList(node):
    start = node
    while(node.next is not start):
        print("{} ".format(node.data),end="")
        node=node.next
  
    # Display the last node of 
    # circular linked list. 
    print("{} ".format(node.data),end="")
  
# Driver Code
if __name__=='__main__':
      
    # Start with empty list 
    head=None
  
    # Using push() function to 
    # convert singly linked list 
    # 17.22.13.14.15 
    head=push(head,15)
    head=push(head,14)
    head=push(head,13)
    head=push(head,22)
    head=push(head,17)
  
    # Call the circular_list function 
    # that convert singly linked 
    # list to circular linked list. 
    circular(head)
    print("Display List:")
    displayList(head)
  
# This Code is Contributed By Vikash Kumar 37

Time complexity: linear
Space complexity: constant
# Python program to exchange first and
# last node in circular linked list     class Node {
class Node:
    def __init__(self):
        self.data = 0
        self.next = None
 
def addToEmpty(head, data):
 
    # This function is only for empty list
    if (head != None):
        return head
 
    # Creating a node dynamically.
    temp = Node()
 
    # Assigning the data.
    temp.data = data
    head = temp
 
    # Creating the link.
    head.next = head
 
    return head
 
def addBegin(head, data):
    if (head == None):
        return addToEmpty(head, data)
 
    temp = Node()
 
    temp.data = data
    temp.next = head.next
    head.next = temp
 
    return head
 
# function for traversing the list
def traverse(head):
 
    # If list is empty, return.
    if (head == None):
        print("List is empty.")
        return
 
    # Pointing to first Node of the list.
    p = head
 
    # Traversing the list.
    while (True):
        print(p.data, end=" ")
        p = p.next
        if(p == head):
            break
 
# Function to exchange first and last node
def exchangeNodes(head):
 
    # If list is of length less than 2
    if (head == None or head.next == None):
        return head
 
    tail = head
 
    # Find pointer to the last node
    while (tail.next != head):
        tail = tail.next
 
    # Exchange first and last nodes using head and p
 
    # temporary variable to store
    # head data
    temp = tail.data
    tail.data = head.data
    head.data = temp
    return head
 
# Driven Program
head = None
head = addToEmpty(head, 6)
 
for i in range(5, 0, -1):
    head = addBegin(head, i)
 
print("List Before: ")
traverse(head)
print("")
 
print("List After: ")
head = exchangeNodes(head)
traverse(head)
 
# This code is contributed by Saurabh Jaiswal

Time complexity: linear
Space complexity: constant