DS Assignment LL
DS Assignment LL
return maxElement;
}
return minElement;
}
if (oddTail!=NULL) {
oddTail->next = evenHead;
}
if (evenTail!=NULL) {
evenTail->next = NULL;
}
if (oddHead!=NULL) {
return oddHead;
} else {
return evenHead;
}
}
Traverse the first linked list (headA) and add elements to the result
list if they are also in headB
Node* current = headA;
while (current != nullptr) {
if (contains(headB, current->data)) {
if (!contains(result_head, current->data)) {
if (result_head == nullptr) {
result_head = new Node(current->data);
result_tail = result_head;
} else {
result_tail->next = new Node(current->data);
result_tail = result_tail->next;
}
}
}
current = current->next;
}
return result_head;
}
Node* findUnion(Node* headA, Node* headB) {
Node* result_head = nullptr;
Node* result_tail = nullptr;
current = headB;
while (current != nullptr) {
if (!contains(result_head, current->data)) {
if (result_head == nullptr) {
result_head = new Node(current->data);
result_tail = result_head;
} else {
result_tail->next = new Node(current->data);
result_tail = result_tail->next;
}
}
current = current->next;
}
return result_head;
}
Q4) Write a function to delete a given linked list
ANS) void deleteLinkedList(ListNode*& head) {
while (head != nullptr) {
ListNode* temp = head;
head = head->next;
delete temp ;
}
}
Q5) Reverse a given linked list with Iterative solution and
recursive solution
ANS) recursive solution-:
Node *reverseLinkedListRec(Node *head)
{
if(head==NULL|| head->next==NULL){
return head;
}
Node *temp=reverseLinkedListRec(head->next);
Node*small=temp;
while(small->next!=NULL){
small=small->next;
}
small->next=head;
head->next=NULL;
return temp;
}
Iterative solution -:
Node *reverseLinkedListiter(Node *head) {
Node* pr=NULL;
Node* temp=head;
Node* fd=temp->next;
if(head==NULL || head->next==NULL){
return head;
}
while(temp!=NULL){
fd=temp->next;
temp->next=pr;
pr=temp;
temp=fd;
}
return pr;
}
Node* pr=NULL;
Node* temp=head;
Node* fd=temp->next;
if(head==NULL || head->next==NULL){
return head;
}
while(temp!=NULL){
fd=temp->next;
temp->next=pr;
pr=temp;
temp=fd;
}
return pr;
}
int mCount = 0;
while (current != nullptr && mCount < m) {
prev = current;
current = current->next;
mCount++;
}
int nCount = 0;
while (current != nullptr && nCount < n) {
Node* temp = current;
current = current->next;
delete temp;
nCount++;
}
prev->next = current;
deleteNNodesAfterMNodes(current, m, n);
return head;
}
if(head==NULL|| head->next==NULL){
return head;
}
Node *temp=reverseLinkedListRec(head->next);
Node*small=temp;
while(small->next!=NULL){
small=small->next;
}
small->next=head;
head->next=NULL;
return temp;
}
Node* addOneToLinkedList(Node* head) {
if (carry > 0) {
prev->next = new Node(carry);
}
return reverseLinkedList(reversedHead);
}
if (prev != nullptr) {
prev->next = nextNode;
}
prev = current;
current = current->next;
}
return newHead;
}
if (next != nullptr) {
head->next = reverseInGroups(next, K);
}
return prev;
}
Q11) Find the Kth element from the end of a given linked
list.
ANS) Node* findKthFromEnd(Node* head, int K) {
if (head == nullptr || K <= 0) {
return nullptr;
}
return slowPtr;
}
// Traverse the first linked list (headA) and add elements to the result
list if they are also in headB
Node* current = headA;
while (current != nullptr) {
if (contains(headB, current->data)) {
if (!contains(result_head, current->data)) {
if (result_head == nullptr) {
result_head = new Node(current->data);
result_tail = result_head;
} else {
result_tail->next = new Node(current->data);
result_tail = result_tail->next;
}
}
}
current = current->next;
}
return result_head;
}
return resultHead;
}
head = head->next;
}
if (thirdLargest != INT_MIN) {
return thirdLargest;
} else {
return -1;
}
}
if (toDelete == nullptr) {
return head;
}
if (prevToDelete == nullptr) {
head = head->next;
delete toDelete;
return head;
}
prevToDelete->next = toDelete->next;
delete toDelete;
return head;
}
if (!hasCycle) {
return nullptr;
}
slowPtr = head;
while (slowPtr != fastPtr) {
slowPtr = slowPtr->next;
fastPtr = fastPtr->next;
}
Q18) Find the middle element of a given linked list without using any
integer variables.
ANS) Node* findMiddle(Node* head) {
if (head == nullptr || head->next == nullptr) {
return head; // Return the head if the list is empty or has only one
element
}
// Move the slow pointer one step at a time, and the fast pointer two
steps at a time
// When the fast pointer reaches the end, the slow pointer will be at
the middle
while (fastPtr != nullptr && fastPtr->next != nullptr) {
slowPtr = slowPtr->next;
fastPtr = fastPtr->next->next;
}
return slowPtr; // Return the middle element
}
delete temp;
head=head->next;
}
}
while (current) {
sum += current->data;
if (sum == 0) {
// A subsequence with a sum of zero found, delete nodes in
between
Node* temp = dummy->next;
while (temp != current) {
sum -= temp->data; // Subtract the node's value from the sum
temp = temp->next;
}
dummy->next = current->next;
}
current = current->next;
}
return dummy->next;
}