Rishab7
New Member
- Joined
- Apr 17, 2023
- Messages
- 21
- Thread Author
- #1
I'm curious if there's a specific approach or logic to reverse a singly-linked list using only two pointers. Typically, the reversal involves the use of three-pointers, namely p, q, and r.
I took reference from this resource. Is there an alternative method for reversing the linked list? What is the most efficient logic for reversing a singly linked list in terms of time complexity?
Code:
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 most efficient logic for reversing a singly linked list in terms of time complexity?