简体   繁体   English

反转链表会影响 main() 中原始链表的头指针。 所以无法比较反向列表和原始列表 - C++

[英]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++

I am writing a program to check whether a singly linked list is a palindrome or not.我正在编写一个程序来检查单向链表是否是回文。 For that, I want to reverse the list, and compare it to the original list.为此,我想反转列表,并将其与原始列表进行比较。 But I am facing the following problem - when I reverse the list, the head pointer of the original list gets modified, and points to NULL.但是我面临以下问题 - 当我反转列表时,原始列表的头指针被修改,并指向 NULL。 So, when I have the following original list, following happens after reversing the original list:因此,当我有以下原始列表时,在反转原始列表后会发生以下情况:

  1. Original list: 1->1->2->1->NULL原始列表:1->1->2->1->NULL
  2. Reversed list: 1->2->1->1->NULL反转列表:1->2->1->1->NULL
  3. But, after calling reverseList, the Original list becomes: 1->NULL但是,在调用 reverseList 后,原始列表变为:1->NULL

This is, because I have the following code to reverse the list:这是因为我有以下代码来反转列表:

 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;
 }

As soon as I do temp1->next = NULL in the above reverseList function (the second last line in the function), the head of the original list is modified and the original list now points to 1->NULL, instead of 1->1->2->1->NULL.只要我在上面的reverseList函数(函数的倒数第二行)中执行temp1->next = NULL ,原始列表的头部就被修改,原始列表现在指向 1->NULL,而不是 1- >1->2->1->NULL。

Below if the full code, that calls the function reverseList:下面是调用函数 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;
 }

So, when the reverseList function returns, following happens in isPalindrome function:因此,当 reverseList 函数返回时, isPalindrome函数中会发生以下情况:

  1. head2 is set to: 1->2->1->1->NULL head2设置为: 1->2->1->1->NULL
  2. head and head1 are set to 1->NULL headhead1设置为1->NULL

And I can no longer compare the two linked lists to check if they are palindromes of each other (since the comparison will give me wrong result).而且我不能再比较两个链表来检查它们是否是彼此的回文(因为比较会给我错误的结果)。

This all is happening because I set temp1->next=NULL in the reverseList function.这一切都发生了,因为我在reverseList函数中设置了temp1->next=NULL

Do you know how I should properly terminate the list in reverseList function, such that it does not effect the original list?你知道我应该如何在reverseList函数中正确终止列表,这样它就不会影响原始列表吗?

Thanks a lot!非常感谢!

Following is the corrected code, where I incorporated the Deep Copy of the original list (in isPalindrome function):以下是更正后的代码,我在其中合并了原始列表的深层副本(在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM