嘗試反轉給定指向鏈表中給定節點的鏈接列表元素。 例如,我會得到一個指向第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;
}
此代碼編譯但沒有正確執行反向節點,我不知道爲什麼。
你能指出故障點嗎?你得到什麼?這不是代碼審查,你需要更具體。 –