在给定指向链表中给定节点的指针的情况下,尝试反转链表元素。例如,我将获得一个指向链表中第 4 个节点的指针和一个指向第 7 个节点的指针,并且必须反转这些节点之间的节点。这是一些不起作用的相关代码:
template <class T>
void List<T>::reverse( ListNode * & startPoint, ListNode * & endPoint ){
if (startPoint == NULL)
return;
if (startPoint->prev != NULL)
startPoint->prev->next = endPoint;
if (endPoint->next != NULL)
endPoint->next->prev = startPoint;
ListNode * curr = startPoint;
ListNode * temp = startPoint;
while(curr != endPoint)
{
temp = curr->next;
curr->next = curr->prev;
curr->prev = temp;
curr = temp;
}
temp = endPoint->next;
endPoint->next = endPoint->prev;
endPoint->prev = temp;
temp = startPoint->next;
startPoint->next = endPoint->next;
endPoint->prev = startPoint->prev;
temp = startPoint;
startPoint = endPoint;
endPoint = temp;
}
此代码编译但没有正确执行反向节点,我不知道为什么。