Skip to content

Linked list insert at position #34

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 2 commits into
base: main
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
Empty file added DSA/__init__.py
Empty file.
Empty file added DSA/number_theory/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions DSA/number_theory/prime_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# prime number is only divisible by 1 and that number itself
# prime number is greater then 1

import math


def is_prime(number: int):
"""Brute force"""
# if number is 1 or less then 1 then its not a prime number
if number <= 1:
return False

# iter every item which starts from 2 and ends before number:
limit = int(math.sqrt(number + 1))
for value in range(2, limit):
# if number is divisible by value: which is number less then the number
# then its not prime number as prime number can't be divided by any other number itself
print("dude!")
if number % value == 0:
return False

if limit <= 2 and number % limit == 0:
return False

return True


number = 3
print(f"Number {number} is prime: {is_prime(number)}")
138 changes: 138 additions & 0 deletions HackerRank/linked_list_insert_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,141 @@ def print_nodes(self):
result.append(current_head.data)
current_head = current_head.next
return result


def push_at(self, newElement, position):

# 1. allocate node to new element
newNode = Node(newElement)

# 2. check if the position is > 0
if position < 1:
print("\nposition should be >= 1.")
elif position == 1:

# 3. if the position is 1, make next of the
# new node as head and new node as head
newNode.next = self.head
self.head = newNode
else:

# 4. Else, make a temp node and traverse to the
# node previous to the position
temp = self.head
for i in range(1, position - 1):
if temp != None:
temp = temp.next

# 5. If the previous node is not null, make
# newNode next as temp next and temp next
# as newNode.
if temp != None:
newNode.next = temp.next
temp.next = newNode
else:

# 6. When the previous node is null
print("\nThe previous node is null.")


# Python3 program for insertion in a single linked
# list at a specified position


# A linked list Node
class Node:
def __init__(self, data):
self.data = data
self.nextNode = None


# function to create and return a Node
def getNode(data):

# allocating space
newNode = Node(data)
return newNode


# function to insert a Node at required position
def insertPos(headNode, position, data):
head = headNode

# This condition to check whether the
# position given is valid or not.
if position < 1:
print("Invalid position!")

if position == 1:
newNode = Node(data)
newNode.nextNode = headNode
head = newNode

else:

# Keep looping until the position is zero
while position != 0:
position -= 1

if position == 1:

# adding Node at required position
newNode = getNode(data)

# Making the new Node to point to
# the old Node at the same position
newNode.nextNode = headNode.nextNode

# Replacing headNode with new Node
# to the old Node to point to the new Node
headNode.nextNode = newNode
break

headNode = headNode.nextNode
if headNode == None:
break
if position != 1:
print("position out of range")
return head


# This function prints contents
# of the linked list
def printList(head):
while head != None:
print(" " + str(head.data), end="")
head = head.nextNode
print()


# Driver Code
if __name__ == "__main__":

# Creating the list 3.5.8.10
head = getNode(3)
head.nextNode = getNode(5)
head.nextNode.nextNode = getNode(8)
head.nextNode.nextNode.nextNode = getNode(10)
print("Linked list before insertion: ", end="")
printList(head)
data = 12
position = 3
head = insertPos(head, position, data)
print("Linked list after insertion of 12 at position 3: ", end="")
printList(head)

# front of the linked list
data = 1
position = 1
head = insertPos(head, position, data)
print("Linked list after insertion of 1 at position 1: ", end="")
printList(head)

# insertion at end of the linked list
data = 15
position = 7
head = insertPos(head, position, data)
print("Linked list after insertion of 15 at position 7: ", end="")
printList(head)

# This code iscontributed by rutvik_56
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

path = os.getcwd()
sys.path.append(path)

4 changes: 4 additions & 0 deletions tests/test_number_theory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_is_prime():
from DSA.number_theory.prime_number import is_prime


Loading