2 ms·
Assuming that by singly linked list, he means that each node only has a pointer to the next node and not to the previous one. For each node, I'd save a pointer
by dawgr 16y ago
Assuming that by singly linked list, he means that each node only has a pointer to the next node and not to the previous one. For each node, I'd save a pointer to it and to the next node. Then replace its next pointer with the pointer to the previous node which should have been saved. Move to the next node and do the same. Done in t(n)=n. If the list was doubly linked, you could do it in half the time by switching 1 and n, 2 and n-1, etc...Would I get the job?
EDIT: also change the list to point to the new first node.