Question: C++ Linked List Swapping Last Two Items Method Trying to swap last two Items of a Singly linked list.. I have this method done below

C++ Linked List Swapping Last Two Items Method

Trying to swap last two Items of a Singly linked list.. I have this method done below to swap the first two items. But I don't understand how to swap the last two of a list

bool ItemList::swapFirstTwo()

{

bool answer = false;

if (m_pHead != nullptr)

{

Item *pFirstNode = m_pHead;

Item *pSecondNode = m_pHead->getNext();

if (pSecondNode != nullptr)

{

m_pHead = pSecondNode;

pFirstNode->setNext(pSecondNode->getNext());

pSecondNode->setNext(pFirstNode);

answer = true;

}

}

return answer;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!