Ap 3
Ap 3
Experiment 3
Student Name: Ankit Vashisth UID: 22BCS13378
Branch: BE-CSE Section/Group: KPIT-901-B
Semester:5th Date of Performance:14/8/24
Subject Name: Advanced Programming Lab-1 Subject Code: 22CSP-314
2. Objective:
3. Implementation:
a) Problem 1:
Given the pointer to the head node of a linked list, change the next pointers of the nodes so that
their order is reversed. The head pointer given may be null meaning that the initial list is empty.
b) Code:
#include <iostream> using
namespace std;
struct ListNode {
int val;
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
return prev;
}
int main() {
ListNode* head = new ListNode(1); head-
>next = new ListNode(2); head->next->next =
new ListNode(3); head->next->next->next =
new ListNode(4);
return 0;
}
c) Output:
a) Problem 2:
To merge two Given Linked List and display them in a sorted manner.
b) Code:
#include <iostream> using
namespace std; struct
ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
}
current = current->next;
} if (l1 != nullptr)
{ current->next =
l1;
} else { current-
>next = l2;
}
ListNode* mergedList = dummy-
>next; delete dummy; return
mergedList;
} void printList(ListNode* head)
{ while (head != nullptr) {
cout << head->val << " -> ";
head = head->next;
}
cout << "nullptr" << endl;
} int
main() {
ListNode* l1 = new ListNode(1);
l1->next = new ListNode(3); l1-
>next->next = new ListNode(5);
return 0;
}
c) Output:
4. Learning Outcomes: