Reversing a Linked List in C++

Katılım
19 Ocak 2023
Mesajlar
9
Excel Vers. ve Dili
I am using Microsoft Excel 2019 in English.
Hello,

I'm having trouble reversing a linked list in C++. I'm trying to use the algorithm outlined in this article but I'm having trouble getting the code to work.

I have the following code, which I believe should work, but is not:

Kod:
struct Node
{
    int data;
    struct Node* next;
};

struct Node* reverse(struct Node* head)
{
    Node* current = head;
    Node* prev = NULL;
    Node* next;
    while (current != NULL)
    {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    head = prev;
    return head;
}
I'm hoping someone can help me figure out what I'm doing wrong and get this code to work.

Thanks in advance!
 

Korhan Ayhan

Administrator
Yönetici
Admin
Katılım
15 Mart 2005
Mesajlar
41,518
Excel Vers. ve Dili
Microsoft 365 Tr-En 64 Bit
Hello,

This is an excel forum. For this reason, you may not be able to get an answer to your question.
 
Üst