Skip to content

Added the Linked List Module to the Hackerrank Interview Prepration Kit with all 5 codes #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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__':
Original file line number Diff line number Diff line change
@@ -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<head.data):
node1.next=head
head.previous=node1
head=node1
return head
temp=head.next
while(temp!=None):
if(temp.prev.data<=node1.data and node1.data<=temp.data):
break
else:
prev=temp
temp=temp.next
if(temp!=None):
node1.prev=temp.prev
node1.next=temp
temp.prev.next=node1
temp.prev=node1
else:
prev.next=node1
node1.prev=prev
return head


Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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