-2

So i am trying to solve a problem to reverse a linked list and following is what i came up with.

class Solution
{
public:

   /* void insertList(struct Node* head, int data)
    {
        Node* temp{ head };
        while (temp->next != nullptr)
        {
            temp = temp->next;
        }
        Node* nextNode{ new Node(data) };
        temp->next = nextNode;
    }*/
    void insertPrev(struct Node* revHead, int data)
    {
        Node* temp{ new Node(data) };
        temp->next = revHead;
        revHead = temp;
    }
    //Function to reverse a linked list.
    struct Node* reverseList(struct Node* head)
    {
        if (head == NULL)
            return head;
        Node* revHead{ new Node(head->data) };
        Node* temp{ head->next };
        while (temp != NULL);//this line
        {
            insertPrev(revHead, temp->data);//this line 2
            temp = temp->next;
        }
        return revHead;
    }
};

when i am trying to run this code to reverse a linked list. it gets stuck infinitely on this line which i have commented as this line. Moreover, ide throws warning also that i may be potentially derefercing a null pointer in this line 2. I don't know how is that possible when i just checked above that temp should not be null for the while loop to run. Help please!

trincot
  • 317,000
  • 35
  • 244
  • 286
  • help please someone – electric fan Jun 07 '23 at 06:20
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Jun 07 '23 at 09:00
  • To reverse a linked list (like `std::list`), just use [std::reverse](https://en.cppreference.com/w/cpp/algorithm/reverse) - one line of code and problem solved. – Jesper Juhl Jun 07 '23 at 10:11
  • You should always post code as a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) that people can cut'n'paste, compile and run. The shown code fails in this regard (for example; there's no `main` function). – Jesper Juhl Jun 07 '23 at 10:23
  • Voting to close as a typo. The `while(temp!= NULL)` is immediately followed by a `;`. So, if `temp` is not equal to `NULL`, that is an infinite loop - and the `{` on the next line is never reached. That would also explain the warning. If the `insertPrev()` call is reached, `temp` is `NULL` and evaluating `temp->data` (and `temp->next` on the next line) gives undefined behaviour. – Peter Jun 07 '23 at 11:26

1 Answers1

1

You have two issues in your code:

  • while (temp != NULL);//this line represents an infinite loop. The semicolon represents an empty body for this while construct. Remove it.

  • insertPrev modifies a local variable revHead: the caller's variable with the same name will not be modified by it. To fix this, define the parameter as pass-by-reference: void insertPrev(struct Node* &revHead, int data) (add the &).

This will fix the issue.

Other remarks

As this is C++ code:

  • Don't declare variables with struct Node*, but just Node*.
  • Don't use a mix of NULL and nullptr: only use nullptr

Depending on what was asked in the challenge, you could solve this by reversing the linked list in place, without needing to create new nodes.

trincot
  • 317,000
  • 35
  • 244
  • 286