I’m trying to implement my own list class but am having trouble reversing just part of my list.
Revelant code:
void List<T>::reverse(ListNode * & head, ListNode * & tail)
{
ListNode* t;
ListNode* curr = head;
ListNode * funtail = tail;
int stop=0;
while(stop==0)
{
if(curr==funtail)
{
stop = 1;
}
t = curr->prev;
curr->prev = curr->next;
curr->next = t;
curr = curr->prev;
}
t = tail;
tail = head;
head = t;
}
If I start with the list
1 2 3 4 5 6 7 8 9 10
and I pass in pointers to 1 and 4, then the list should look like
4 3 2 1 5 6 7 8 9 10
The problem is, my list returns as just
1
with the rest of the list lost (well, still accessible from my global tail variable). Any ideas? Is my method wrong?
Your
head->prevmust be pointing toNULLin first for loop . You better think and implement digramatically it will be helpful . You needt->next->next =t->next.