Jeetansh Arora - Assignment (Singly Linked List)
Jeetansh Arora - Assignment (Singly Linked List)
Q1. Form a list containing the UNION of the elements of two lists (singly
linked list).
def union(list1, list2):
result = set(list1)
for item in list2:
result.add(item)
return list(result)
Q2. Form a list containing the INTERSECTION of the elements of two lists
(singly linked list).
def intersection(list1, list2):
set1 = set(list1)
set2 = set(list2)
return list(set1.intersection(set2))
Q3. Insert an element after the nth element of a list (singly linked list).
class Node:
def __init__(self, data):
self.data = data
self.next = None
def insert_after_nth(head, n, data):
current = head
count = 1
while count < n and current:
current = current.next
count += 1
if current:
new_node = Node(data)
new_node.next = current.next
current.next = new_node
Q4. Place the elements of a list in increasing order (singly linked list).
def sort_linked_list(head):
values = []
current = head
while current:
values.append(current.data)
current = current.next
values.sort()
current = head
for value in values:
current.data = value
current = current.next
Q8. Move node (p) forward n positions in the list (circular doubly linked
list).
def move_node_forward(head, node, n):
current = head
while current and current != node:
current = current.next
if not current:
return
for _ in range(n):
current = current.next
node.data, current.data = current.data, node.data
Q9. Recursive function to find the number of nodes in a linked list (singly
linked list).
def count_nodes_recursive(node):
if not node:
return 0
return 1 + count_nodes_recursive(node.next)
Q15. Take the PARTIAL DERIVATIVE of polynomial with respect to any of its
variables (doubly linked list).
#include <stdio.h>
#include <stdlib.h>
int coeff, x, y, z;
} Node;
newNode->coeff = coeff;
newNode->x = x;
newNode->y = y;
newNode->z = z;
newNode->next = head;
newNode->prev = NULL;
return newNode;
while (temp) {
if ((var == 'x' && temp->x > 0) || (var == 'y' && temp->y > 0) || (var == 'z' && temp->z > 0)) {
temp = temp->next;
}
return result;
while (head) {
head = head->next;
printf("\n");
int main() {
display(poly);
display(deriv);
return 0;
#include <stdlib.h>
} Node;
newNode->coeff = coeff;
newNode->exp = exp;
if (!head) {
newNode->next = newNode;
return newNode;
temp->next = newNode;
newNode->next = head;
return head;
}
Node* divide(Node* dividend, Node* divisor, Node** remainder) {
*remainder = dividend;
while (tempDiv) {
tempDiv = tempDiv->next;
while (tempRem) {
int found = 0;
while (tempSub) {
if (tempSub->exp == tempRem->exp) {
found = 1;
break;
tempSub = tempSub->next;
tempRem = tempRem->next;
*remainder = newRem;
if (!(*remainder)) break;
return quotient;
if (!head) {
printf("0\n");
return;
do {
printf("\n");
int main() {
printf("Dividend: ");
display(dividend);
printf("Divisor: ");
display(divisor);
printf("Quotient: ");
display(quotient);
printf("Remainder: ");
display(remainder);
return 0;