Microsoft Corporation Question:
Download Job Interview Questions and Answers PDF
How would you reverse a doubly-linked list?
Answer:
This problem isn't too hard. You just need to start at the head of the list, and iterate to the end. At each node, swap the values of pNext and pPrev. Finally, set pHead to the last node in the list.
Node * pCurrent = pHead, *pTemp;
while (pCurrent)
{ pTemp = pCurrent-gt;pNext;
pCurrent-gt;pNext = pCurrent->pPrev;
pCurrent-gt;pPrev = temp;
pHead = pCurrent;
pCurrent = temp;
}
Node * pCurrent = pHead, *pTemp;
while (pCurrent)
{ pTemp = pCurrent-gt;pNext;
pCurrent-gt;pNext = pCurrent->pPrev;
pCurrent-gt;pPrev = temp;
pHead = pCurrent;
pCurrent = temp;
}
Download Microsoft Interview Questions And Answers
PDF
Previous Question | Next Question |
Microsoft Interview Questions List | How could you determine if a linked list contains a cycle in it, and, at what node the cycle starts? |