Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате |
Форум программистов > Другие языки > Reverse singly linked list using only two pointers |
Автор: Aviral20 14.12.2023, 14:26 |
I wonder if there exists some logic to reverse a singly-linked list using only two pointers. The following is used to reverse the single linked list using three pointers namely p, q, 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 https://www.interviewbit.com/blog/reverse-a-linked-list/. Is there an alternative method to reverse the linked list? What is the optimal logic for reversing a singly linked list in terms of time complexity? |