Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started