This question has already been answered:
- Theory on a single-linked list 2 answers
How can I deploy it using O (n) time and O (1) memory?
strunct Node { int payload; Node *next; }; Node* head; This question has already been answered:
How can I deploy it using O (n) time and O (1) memory?
strunct Node { int payload; Node *next; }; Node* head; A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .
Insert at the beginning of the list O (1) in time and memory. Therefore, you can do this: get a new empty list, bypass the old one by inserting each element at the beginning of a new one. Total it will turn out just O (1) on memory and O (n) on time.
Without inserting into a new one, just go through the list, expanding the field next each element, to the previous one. For the former first one, the next field is reset, head set to the former last element.
In fact, in general, the same thing as inserting into a new one ..
Source: https://ru.stackoverflow.com/questions/514562/
All Articles