From a21db15236cef3ec65f6ad5121a5157445ef5ea7 Mon Sep 17 00:00:00 2001 From: Ayush Mishra <56378335+ayushofficial07@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:18:12 +0530 Subject: [PATCH 1/5] Create Insert_a_node_at_a_specific_position_in_a_linked_list.py --- ...at_a_specific_position_in_a_linked_list.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Interview Preparation Kit - Python/Linked Lists/Insert_a_node_at_a_specific_position_in_a_linked_list.py 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__': From 395f050a0358a53f8e6a45db670276c85ee8b925 Mon Sep 17 00:00:00 2001 From: Ayush Mishra <56378335+ayushofficial07@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:22:40 +0530 Subject: [PATCH 2/5] Create Inserting a Node Into a Sorted Doubly Linked List.py --- ...a Node Into a Sorted Doubly Linked List.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Interview Preparation Kit - Python/Linked Lists/Inserting a Node Into a Sorted Doubly Linked List.py 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 Date: Fri, 2 Oct 2020 12:26:13 +0530 Subject: [PATCH 3/5] Create Reverse a doubly linked list.py --- .../Reverse a doubly linked list.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Interview Preparation Kit - Python/Linked Lists/Reverse a doubly linked list.py diff --git a/Interview Preparation Kit - Python/Linked Lists/Reverse a doubly linked list.py b/Interview Preparation Kit - Python/Linked Lists/Reverse a doubly linked list.py new file mode 100644 index 0000000..24c7eec --- /dev/null +++ b/Interview Preparation Kit - Python/Linked Lists/Reverse a doubly linked list.py @@ -0,0 +1,27 @@ + + +# Complete the reverse function below. + +# +# For your reference: +# +# DoublyLinkedListNode: +# int data +# DoublyLinkedListNode next +# DoublyLinkedListNode prev +# +# +def reverse(head): + if(head==None or head.next==None): + return head + else: + temp=head + temp1=None + while temp is not None: + temp1 = temp.prev; + temp.prev = temp.next; + temp.next = temp1; + temp = temp.prev; + head1=temp1.prev + return head1 + From e327ee6fac4b48584ffa1dda4c6d6a92a2448a18 Mon Sep 17 00:00:00 2001 From: Ayush Mishra <56378335+ayushofficial07@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:28:00 +0530 Subject: [PATCH 4/5] Create Find Merge Point of Two Lists.py --- .../Find Merge Point of Two Lists.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Interview Preparation Kit - Python/Linked Lists/Find Merge Point of Two Lists.py 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 + From 196c8a0485fd623d1152df49a21bf3dcd57ec002 Mon Sep 17 00:00:00 2001 From: Ayush Mishra <56378335+ayushofficial07@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:32:24 +0530 Subject: [PATCH 5/5] Create Linked Lists: Detect a Cycle.py --- .../Linked Lists: Detect a Cycle.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Interview Preparation Kit - Python/Linked Lists/Linked Lists: Detect a Cycle.py diff --git a/Interview Preparation Kit - Python/Linked Lists/Linked Lists: Detect a Cycle.py b/Interview Preparation Kit - Python/Linked Lists/Linked Lists: Detect a Cycle.py new file mode 100644 index 0000000..98ce42a --- /dev/null +++ b/Interview Preparation Kit - Python/Linked Lists/Linked Lists: Detect a Cycle.py @@ -0,0 +1,23 @@ +""" +Detect a cycle in a linked list. Note that the head pointer may be 'None' if the list is empty. + +A Node is defined as: + + class Node(object): + def __init__(self, data = None, next_node = None): + self.data = data + self.next = next_node +""" + + +def has_cycle(head): + temp=[] + travel=head + while(travel!=None): + if(travel in temp): + return True + else: + temp.append(travel) + travel=travel.next + return False +