0% found this document useful (0 votes)
4 views

Apexp3

The document outlines two experiments in a Computer Science and Engineering lab focusing on linked list operations in C++. The first experiment involves merging two sorted linked lists using an efficient algorithm, while the second experiment focuses on removing duplicate nodes from a sorted linked list. Both experiments emphasize optimizing space and time complexity, as well as enhancing coding skills for technical interviews.

Uploaded by

Jain Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Apexp3

The document outlines two experiments in a Computer Science and Engineering lab focusing on linked list operations in C++. The first experiment involves merging two sorted linked lists using an efficient algorithm, while the second experiment focuses on removing duplicate nodes from a sorted linked list. Both experiments emphasize optimizing space and time complexity, as well as enhancing coding skills for technical interviews.

Uploaded by

Jain Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 3
Student Name: Jain Aman UID: 22BCS14831
Branch: BE-CSE Section/Group: 637/B
Semester: 6th Date of Performance: 29/01/2025
Subject Name: AP LAB-II
Subject Code: 22CSP-351

Problem:01

1. Aim:

To implement and understand the merging of two sorted linked lists using an efficient
algorithm in C++.

2. Objective:

• To develop an efficient algorithm for merging two sorted linked lists.


• To understand the use of linked list traversal and node manipulation.
• To optimize space and time complexity while solving linked list problems.

3. Implementation/Code:

class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if (!list1) return list2;
if (!list2) return list1;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

ListNode* head = new ListNode(0); // Dummy node


ListNode* current = head;

while (list1 && list2) {


if (list1->val <= list2->val) {
current->next = list1;
list1 = list1->next;
} else {
current->next = list2;
list2 = list2->next;
}
current = current->next;
}

if (list1) current->next = list1;


if (list2) current->next = list2;

return head->next; // Return the merged list starting from the first actual node
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Output:

5. Learning Outcome:

• Understand how to merge two sorted linked lists efficiently.


• Implement linked list traversal and node manipulation in C++.
• Optimize space and time complexity for linked list operations.
• Develop problem-solving skills for linked list-based challenges.
• Enhance coding skills for technical interviews and competitive programming.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem:02

1.
Aim: To remove all nodes with duplicate values from a sorted linked list, ensuring
that only distinct numbers remain in the final sorted list.

2.
Objective:

• To traverse a sorted linked list and identify duplicate nodes.


• To efficiently remove all occurrences of duplicate values while maintaining list
order.
• To implement an optimized algorithm with minimal extra space usage.
3.
Implementation/Code:

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head || !head->next) return head;

ListNode* dummy = new ListNode(0, head); // Dummy node before the head
ListNode* prev = dummy;

while (head) {
// Check if it's a start of duplicates
if (head->next && head->val == head->next->val) {
// Skip all duplicates
while (head->next && head->val == head->next->val) {
head = head->next;
}
prev->next = head->next; // Remove duplicates
} else {
prev = prev->next; // Move forward
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

head = head->next; // Move to next node


}

return dummy->next; // Return modified list


}
};

4. Output:

5. Learning Outcome:

• Understand how to detect and remove duplicate nodes from a sorted linked list.
• Implement pointer manipulation for modifying linked lists efficiently.
• Optimize space and time complexity for duplicate removal operations.
• Develop problem-solving skills in linked list-based challenges.
• Improve coding proficiency for technical interviews and competitive
programming.
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

You might also like