diff --git a/Interview Preparation Kit - Python/Linked Lists/Find Merge Point of Two Lists.py b/Interview Preparation Kit - Python/Linked Lists/Find Merge Point of Two Lists.py new file mode 100644 index 0000000..f4b1364 --- /dev/null +++ b/Interview Preparation Kit - Python/Linked Lists/Find Merge Point of Two Lists.py @@ -0,0 +1,26 @@ + + +# Complete the findMergeNode function below. + +# +# For your reference: +# +# SinglyLinkedListNode: +# int data +# SinglyLinkedListNode next +# +# +def findMergeNode(head1, head2): + temp1=head1 + temp2=head2 + while(temp1!=temp2): + if(temp1.next==None): + temp1=head2 + else: + temp1=temp1.next + if(temp2.next==None): + temp2=head1 + else: + temp2=temp2.next + return temp2.data + diff --git a/Interview Preparation Kit - Python/Linked Lists/Insert_a_node_at_a_specific_position_in_a_linked_list.py b/Interview Preparation Kit - Python/Linked Lists/Insert_a_node_at_a_specific_position_in_a_linked_list.py new file mode 100644 index 0000000..64503ea --- /dev/null +++ b/Interview Preparation Kit - Python/Linked Lists/Insert_a_node_at_a_specific_position_in_a_linked_list.py @@ -0,0 +1,62 @@ +#!/bin/python3 + +import math +import os +import random +import re +import sys + +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) + +# Complete the insertNodeAtPosition function below. + +# +# For your reference: +# +# SinglyLinkedListNode: +# int data +# SinglyLinkedListNode next +# +# +def insertNodeAtPosition(head, data, position): + counter=1 + node1=SinglyLinkedListNode(data) + temp=head + while(counter!=position): + temp=temp.next + counter+=1 + node1.next=temp.next + temp.next=node1 + return head + + + +if __name__ == '__main__': diff --git a/Interview Preparation Kit - Python/Linked Lists/Inserting a Node Into a Sorted Doubly Linked List.py b/Interview Preparation Kit - Python/Linked Lists/Inserting a Node Into a Sorted Doubly Linked List.py new file mode 100644 index 0000000..6334bd8 --- /dev/null +++ b/Interview Preparation Kit - Python/Linked Lists/Inserting a Node Into a Sorted Doubly Linked List.py @@ -0,0 +1,39 @@ + + +# Complete the sortedInsert function below. + +# +# For your reference: +# +# DoublyLinkedListNode: +# int data +# DoublyLinkedListNode next +# DoublyLinkedListNode prev +# +# +def sortedInsert(head, data): + prev=None + node1=DoublyLinkedListNode(data) + if(node1.data