Forums » Outras Discussões

How to reverse a singly linked list using only two pointers?

    • 10 posts
    14 de dezembro de 2023 08:10:49 ART
    I'm curious if there's a method to reverse a singly-linked list using only two pointers. The conventional approach to reversing a singly linked list involves utilizing three-pointers, namely p, q, and r: struct node { int data; struct node *link; }; void reverse() { struct node *p = first, *q = NULL, *r; while (p != NULL) { r = q; q = p; p = p->link; q->link = r; } first = q; } I took reference from this resource. Is there an alternative method for reversing the linked list? What is the optimal logic for reversing a singly linked list, considering time complexity?
    Este post foi editado por Ashley cyruss em 14 de dezembro de 2023 08:15:50 ART"