4. Print LL in reverse #
Created Saturday 18 January 2020
- If uni or empty, return;
- Solve using stack. Better use recursion. But we could also make an LL.
- Iterative
- Make a node pointer named phead
- Traverse the original LL till head equals NULL.
- create a new node with data as (head).
- newnode->next = phead; // first one points to NULL, as it’ll be the last.
- phead = newnode, where we will point in the next iteration.
- At the end, there would be no iteration.
- phead is the head of the reversed linked list(as in-place reversing was not allowed on the original list, otherwise we would have reversed it in the first place).
- Traverse the reversedLL, create a to_be_deleted pointer.
- to_be_deleted = phead;
- phead = phead -> next;
- cout << to_be_deleted << " ";
- delete to_be_deleted.
- Recursive solution:
if(head==NULL) return; f(head->next); cout << head ->data;