Skip to content
This repository was archived by the owner on May 13, 2026. It is now read-only.
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
72 changes: 72 additions & 0 deletions Q2/Q2_Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class Node:

# Function to initialise the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null

class LinkedList:

# Function to initialize head
def __init__(self):
self.head = None

# Function to add node at the end
def create(self, x):

new_node = Node(x)

if self.head is None:
self.head = new_node
return

last = self.head
while last.next:
last = last.next

last.next = new_node

# This function prints contents of linked
# list starting from the given node
def display(self):
temp = self.head

while temp:
print(temp.data, end = " ")
temp = temp.next

# Function to remove nth node from last
def removeNthFromEnd(head, k):

# the function uses two pointer method
first = head
second = head
count = 1
while count <= k:
second = second.next
count += 1
if second is None:
head.value = head.next.value
head.next = head.next.next
return
while second.next is not None:
first = first.next
second = second.next
first.next = first.next.next

# Driver code

# val list contains list of values
val = [1, 2, 3, 4, 5]
k = 2
ll = LinkedList()
for i in val:
ll.create(i)

print("Linked list before modification:");
ll.display()

removeNthFromEnd(ll.head, k)

print("\nLinked list after modification:");
ll.display()
8 changes: 0 additions & 8 deletions Q2/solution.py

This file was deleted.