compare two linked list python

class SinglyLinkedListNode:
    def __init__(self, node_data):
        self.data = node_data
        self.next = None


class SinglyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def insert_node(self, node_data):
        node = SinglyLinkedListNode(node_data)

        if not self.head:
            self.head = node
        else:
            self.tail.next = node

        self.tail = node


def print_singly_linked_list(node, sep, fptr):
    while node:
        fptr.write(str(node.data))

        node = node.next

        if node:
            fptr.write(sep)


def printt(headd):
    itr = headd
    llstr = []
    while itr:
        llstr.append(itr.data)
        itr = itr.next
    return llstr


def compare_lists(llist1, llist2):
    ll1 = printt(llist1)
    ll2 = printt(llist2)
    if ll1 == ll2:
        return 'Same'
    else:
        return 'Not Same'


if __name__ == '__main__':

    llist1_count = int(input("Number of nodes in LinkedList1: "))

    llist1 = SinglyLinkedList()
    print("Enter the value to be stored on linkedlist 1: ")
    for _ in range(llist1_count):
        llist1_item = input()
        llist1.insert_node(llist1_item)

    print('\n')
    llist2_count = int(input("Number of nodes in LinkedList2: "))

    llist2 = SinglyLinkedList()

    print("Enter the value to be stored on linkedlist 2: ")
    for _ in range(llist2_count):
        llist2_item = input()
        llist2.insert_node(llist2_item)

    result = compare_lists(llist1.head, llist2.head)

    print('Result:', result)

    '''
    Explanation 1:
    ----------------------------------------------
    Input format:
    Number of nodes in LinkedList1: 2
    Enter the value to be stored on linkedlist 1:
    1
    2
    Number of nodes in LinkedList2: 2
    Enter the value to be stored on linkedlist 2:
    1
    2
    -------------------------------------------------
    Output:
    Result: Same
    --------------------------------------------------
    Explanation 2:
    ----------------------------------------------
    Input format:
    Number of nodes in LinkedList1: 2
    Enter the value to be stored on linkedlist 1:
    1
    2
    Number of nodes in LinkedList2: 2
    Enter the value to be stored on linkedlist 2:
    2
    1
    -------------------------------------------------
    Output:
    Result: Not Same
    --------------------------------------------------
    '''

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source