Link List Solution 1st Level
Link List Solution 1st Level
h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
copyCurrent->next = createNode(current->data);
copyCurrent = copyCurrent->next;
current = current->next;
Return newHead;
Head = head->next;
Printf(“NULL\n”);
Int main() {
Head->next = createNode(2);
Head->next->next = createNode(3);
Printf(“Original list:\n”);
printList(head);
printf(“Copied list:\n”);
printList(copiedList);
return 0;
2.#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
Int count = 0;
Count++;
Head = head->next;
Return count;
}
Int main() {
Head->next = createNode(2);
Head->next->next = createNode(3);
Return 0;
3.#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to search for an element in a linked list
Int position = 0;
If (head->data == key) {
Return position;
Head = head->next;
Position++;
Int main() {
Head->next = createNode(2);
Head->next->next = createNode(3);
Int key = 2;
If (position != -1) {
} else {
}
Return 0;
4.#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
// Function to create a new list using alternating nodes from the given list
Current = current->next->next;
If (current != NULL) {
newCurrent->next = createNode(current->data);
newCurrent = newCurrent->next;
Return newHead;
Head = head->next;
Printf(“NULL\n”);
Int main() {
// Creating a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Head->next = createNode(2);
Head->next->next = createNode(3);
Head->next->next->next = createNode(4);
Head->next->next->next->next = createNode(5);
Printf(“Original list:\n”);
printList(head);
printList(altList);
return 0;
5.#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
*firstHalf = head;
*secondHalf = NULL;
Return;
Prev = slow;
Slow = slow->next;
Fast = fast->next->next;
*firstHalf = head;
*secondHalf = slow;
If (prev != NULL) {
Prev->next = NULL;
}
// Utility function to print a linked list
Head = head->next;
Printf(“NULL\n”);
Int main() {
// Creating a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Head->next = createNode(2);
Head->next->next = createNode(3);
Head->next->next->next = createNode(4);
Head->next->next->next->next = createNode(5);
printf(“First half:\n”);
printList(firstHalf);
printf(“Second half:\n”);
printList(secondHalf);
return 0;
6.#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
Result = list1;
Result->next = mergeSortedLists(list1->next, list2);
} else {
Result = list2;
Return result;
Head = head->next;
Printf(“NULL\n”);
Int main() {
List1->next = createNode(3);
List1->next->next = createNode(5);
List2->next = createNode(4);
List2->next->next = createNode(6);
Printf(“List 1:\n”);
printList(list1);
printf(“List 2:\n”);
printList(list2);
printList(mergedList);
return 0;
7.#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to concatenate two linked lists
Temp = temp->next;
Temp->next = list2;
Return list1;
Head = head->next;
Printf(“NULL\n”);
Int main() {
List1->next = createNode(2);
List1->next->next = createNode(3);
Struct Node* list2 = createNode(4);
List2->next = createNode(5);
List2->next->next = createNode(6);
Printf(“List 1:\n”);
printList(list1);
printf(“List 2:\n”);
printList(list2);
printf(“Concatenated list:\n”);
printList(concatenatedList);
return 0;
8.#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->next = NULL;
return newNode;
If (list1->data != list2->data) {
List1 = list1->next;
List2 = list2->next;
Int main() {
List1->next = createNode(2);
List1->next->next = createNode(3);
Struct Node* list2 = createNode(1);
List2->next = createNode(2);
List2->next->next = createNode(3);
If (compareLists(list1, list2)) {
} else {
Return 0;
9.#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to count the occurrences of a given number in the list
Int count = 0;
If (head->data == key) {
Count++;
Head = head->next;
Return count;
Int main() {
// Creating a sample linked list: 1 -> 2 -> 3 -> 2 -> 5 -> 2 -> NULL
Head->next = createNode(2);
Head->next->next = createNode(3);
Head->next->next->next = createNode(2);
Head->next->next->next->next = createNode(5);
Head->next->next->next->next->next = createNode(2);
Int key = 2;
Return 0;
}
10.#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
Return prev;
Head = head->next;
Printf(“NULL\n”);
Int main() {
// Creating a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Head->next = createNode(2);
Head->next->next = createNode(3);
Head->next->next->next = createNode(4);
Head->next->next->next->next = createNode(5);
Printf(“Original list:\n”);
printList(head);
head = reverseList(head);
printf(“Reversed list:\n”);
printList(head);
return 0;