anyone able to help me with linkedlist?
I will help u
Where is the question
reversing a linkedlist
You can reverse it in place, you don't need to make a copy.
lol
i dunno
srry
but i want to copy it ... then reverse it
Do you understand the solution I posted above?
And do you understand why it is not necessary to copy it.
You are wasting both time and space (computation time and memory).
well the solution above does not pass the tests...
Hrmm.
Can you post the code you have right now?
http://gyazo.com/836d2b49fdbf9bce2db148a1e46af33d http://gyazo.com/c3d08e2f3f342942401adbc4d70da350
Link prev = NULL; Link next = NULL; Link curr = L->first; while(curr) { next = curr->next; curr->next = prev; prev = curr; curr = next; } curr = L->first; L->first = L->last; L->last = curr;
Does that fix it?
can u draw me a pic what ur code is doing
Hrmm I think it is easier just to explain it in english.
If you have a bunch of nodes and arrows pointing forward from one node to the next. The idea is to reverse the direction of the arrows.
Lets say we have three nodes, node[i - 1], node[i] and node[i + 1] and we are dealing with node[i]. When we reach node[i] it is currently pointing to node[i + 1], in other words node[i]->next = node[i + 1]. We want it to point to node[i - 1] instead. That's why we have curr->next = prev.
oh ok cool, then u start with the last entry and deal with reversed links?
|dw:1417824019058:dw|
Join our real-time social learning platform and learn together with your friends!