Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (mimi_x3):

anyone able to help me with linkedlist?

OpenStudy (mimi_x3):

why doesnt this work? http://gyazo.com/8b1938c77649f36c910eabb8387bfee0

OpenStudy (mimi_x3):

this is the struct http://gyazo.com/dc16aa2d5c15e51ab05f04f679d381d9

OpenStudy (anonymous):

I will help u

OpenStudy (anonymous):

Where is the question

OpenStudy (mimi_x3):

reversing a linkedlist

OpenStudy (anonymous):

You can reverse it in place, you don't need to make a copy.

OpenStudy (dan815):

lol

OpenStudy (anonymous):

i dunno

OpenStudy (anonymous):

srry

OpenStudy (mimi_x3):

but i want to copy it ... then reverse it

OpenStudy (anonymous):

Do you understand the solution I posted above?

OpenStudy (anonymous):

And do you understand why it is not necessary to copy it.

OpenStudy (anonymous):

You are wasting both time and space (computation time and memory).

OpenStudy (mimi_x3):

well the solution above does not pass the tests...

OpenStudy (anonymous):

Hrmm.

OpenStudy (anonymous):

Can you post the code you have right now?

OpenStudy (mimi_x3):

this is what it does http://gyazo.com/d886cd04788c417c5220cbaf847e6e6c

OpenStudy (anonymous):

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;

OpenStudy (anonymous):

Does that fix it?

OpenStudy (dan815):

can u draw me a pic what ur code is doing

OpenStudy (anonymous):

Hrmm I think it is easier just to explain it in english.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (dan815):

oh ok cool, then u start with the last entry and deal with reversed links?

OpenStudy (anonymous):

|dw:1417824019058:dw|

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!