[英]Reversing a linked list effects the head pointer of the original list in main(). So not able to compare the reversed list and original list - C++
我正在編寫一個程序來檢查單向鏈表是否是回文。 為此,我想反轉列表,並將其與原始列表進行比較。 但是我面臨以下問題 - 當我反轉列表時,原始列表的頭指針被修改,並指向 NULL。 因此,當我有以下原始列表時,在反轉原始列表后會發生以下情況:
這是因為我有以下代碼來反轉列表:
ListNode* reverseList(ListNode* head)
{
ListNode* temp = head;
ListNode* temp1 = temp;
ListNode* current = NULL, * nextNode = NULL;
if (temp)
current = temp->next;
if (current)
nextNode = current->next;
while (current)
{
current->next = temp;
temp = current;
current = nextNode;
if (current)
nextNode = current->next;
}
temp1->next = NULL;
return temp;
}
只要我在上面的reverseList
函數(函數的倒數第二行)中執行temp1->next = NULL
,原始列表的頭部就被修改,原始列表現在指向 1->NULL,而不是 1- >1->2->1->NULL。
下面是調用函數 reverseList 的完整代碼:
struct ListNode
{
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){}
};
ListNode* reverseList(ListNode* head)
{
ListNode* temp = head;
ListNode* temp1 = temp;
ListNode* current = NULL, * nextNode = NULL;
if (temp)
current = temp->next;
if (current)
nextNode = current->next;
while (current)
{
current->next = temp;
temp = current;
current = nextNode;
if (current)
nextNode = current->next;
}
temp1->next = NULL;
return temp;
}
bool isPalindrome(ListNode* head) {
//reverse the Linked list and then compare the two lists.
if (head == NULL)
return true;
ListNode* head1 = head;
ListNode* head2 = reverseList(head);
while (head1 && head2)
{
if (head1->val != head2->val)
return false;
head1 = head1->next;
head2 = head2->next;
}
return true;
}
int main()
{
ListNode* head = new ListNode(1);
head->next = new ListNode(1);
head->next->next = new ListNode(2);
head->next->next->next = new ListNode(1);
head->next->next->next->next = NULL;
bool palindrome = isPalindrome(head);
cout << palindrome << endl;
return 0;
}
因此,當 reverseList 函數返回時, isPalindrome
函數中會發生以下情況:
head2
設置為: 1->2->1->1->NULL
head
和head1
設置為1->NULL
而且我不能再比較兩個鏈表來檢查它們是否是彼此的回文(因為比較會給我錯誤的結果)。
這一切都發生了,因為我在reverseList
函數中設置了temp1->next=NULL
。
你知道我應該如何在reverseList
函數中正確終止列表,這樣它就不會影響原始列表嗎?
非常感謝!
以下是更正后的代碼,我在其中合並了原始列表的深層副本(在isPalindrome
函數中):
struct ListNode
{
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){}
};
ListNode* reverseList(ListNode* head)
{
ListNode* temp = head;
ListNode* temp1 = temp;
ListNode* current = NULL, * nextNode = NULL;
if (temp)
current = temp->next;
if (current)
nextNode = current->next;
while (current)
{
current->next = temp;
temp = current;
current = nextNode;
if (current)
nextNode = current->next;
}
temp1->next = NULL;
return temp;
}
bool isPalindrome(ListNode* head) {
//reverse the Linked list and then compare the two lists.
if (head == NULL)
return true;
ListNode* head1 = head;
ListNode* temp1 = NULL, *temp2=NULL;
bool firstEntry = true;
//Deep Copy
temp2 = temp1 = new ListNode(head1->val);
while (head1->next)
{
temp1->next = new ListNode(head1->next->val);
temp1 = temp1->next;
head1 = head1->next;
}
temp1->next = NULL;
ListNode* head2 = reverseList(head);
while (temp2 && head2)
{
if (temp2->val != head2->val)
return false;
temp2 = temp2->next;
head2 = head2->next;
}
return true;
}
int main()
{
ListNode* head = new ListNode(1);
head->next = new ListNode(1);
head->next->next = new ListNode(2);
head->next->next->next = new ListNode(1);
head->next->next->next->next = NULL;
bool palindrome = isPalindrome(head);
cout << palindrome << endl;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.