Hi,
I'm having trouble reversing a linked list in my code. I'm using the algorithm outlined in the article "Reverse a Linked List" found here. I'm having trouble understanding the concept of the algorithm and how to properly implement it in my code.
Here's my code so far:
struct Node {
int data;
Node *prev;
Node *next;
};
Node* reverseLinkedList(Node *head) {
Node *cur = head;
Node *prev = NULL;
Node *next = NULL;
while (cur != NULL) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
head = prev;
return head;
}
Any advice on how to properly implement this algorithm in my code would be greatly appreciated.
Thank you!
Unpublished comment
Viết câu trả lời