Home arrow static arrow Java Programming [Archive] - Reversing LinkedList elements
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Reversing LinkedList elements
This topic has 142 replies on 10 pages.    « Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next »

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 8:56 AM (reply 15 of 142)



 

This is a double linked list. So if you're at the end,
you just need to read the previous one.

if(current = last) //if Link points at the last position

last = first.getNext(); // then change the next of last to first.getNext()

for (current != null)

previous = current.getPrevious();

current.next() = previous;

Is this what u're saying?
 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 9:05 AM (reply 16 of 142)



 
My answer had suffient information, including when to stop and consider.

Don't worry, you'll get there.

BTW I'm kinda curious about the Annihilator nick. What are you annihilating? Is it goodwill?

/k1
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 9:11 AM (reply 17 of 142)



 

BTW I'm kinda curious about the Annihilator nick. What
are you annihilating? Is it goodwill?

i dont' care much about the nick or rep in this fora, i just registered the nick whatever came first
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 9:12 AM (reply 18 of 142)



 
My answer had suffient information, including when to
stop and consider.

could your please cosider making life easier atleast at this stage?

i would appreciate it.

comment the lines in the code given with apt explanation!?

 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 9:23 AM (reply 19 of 142)



 
i'll ease up if u think i am overly dominating mutual exchanges of comments
 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 9:26 AM (reply 20 of 142)



 
Believe it or not that is exactly what I've been trying to do.

You appear (to me at least) to lack any sense of design judgement or the ability to look at your problems from the outside of your latest emergency (this appears to happen a few times every day).

Being attentive to detail is a fantastic attribute for a coder. Much more important is to understand what your real goals are, and why you need to achieve them.

To my mind, you are still focused far too much on the detail, not the task. As a result, you will forever face the same sorts of issues arising in different contexts and not be able to use your previous experience to help yourself to resolve new issues, new emergencies.

Basically, my assessment is that you understand syntax but not semantics.

If you want my opinion, which I'm sure you do not, then you should re-evaluate your priorities and start wondering how you can begin thinking like a developer.

/k1
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 9:36 AM (reply 21 of 142)



 
my resources at this point are limited
therefore the questions
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 9:41 AM (reply 22 of 142)



 
i'll have to agree what u say upto certain extent but even then let's evaluate each line in the OP together
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 9:44 AM (reply 23 of 142)



 
i'll have to agree what u say upto certain extent but
even then let's evaluate each line in the OP together

Annihilator890i, do you have a debugger? Create a list, and add the values 1,2,3,4 to it. Then run the code in the debugger, and see what it does. It's a great way to learn. If you doesn't have a debugger, just add a lot of System.out to the code, and run it.

/Kaj
 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 9:55 AM (reply 24 of 142)



 
my resources at this point are limited
then use them wisely.
 

Posts:5,965
Registered: 5/17/03
Re: Reversing LinkedList elements  
Aug 8, 2004 9:56 AM (reply 25 of 142)



 
This is based on my reply 5. You remove the nodes one by one from the straight list and add them at the very beginning of the reversed list. It isn't tested but should work.
public Node reverse(Node straight) {    Node reversed = null; // start of reversed list   while (straight != null) { // straight list has become empty      Node p = straight; // remove first node of straight list      straight = straight.next;      p.next = reversed; // and add first in reversed list      reversed = p:   }   return reversed;}
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 9:57 AM (reply 26 of 142)



 
Hey /Kaj

I inserted sys.print but i only get hashcode i.e. memory address

hard to differentiate

anyother way?

do you understand the code in OP ?

thanks
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 10:02 AM (reply 27 of 142)



 

public Node reverse(Node straight) {Node reversed = null; // start of reversed listwhile (straight != null) { // straight list has becomeemptyNode p = straight; // remove first node of straightliststraight = straight.next;p.next = reversed; // and add first in reversed listreversed = p:}return reversed;}

can you tell me whats the
".next"
for?
is .next a field?
 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 10:07 AM (reply 28 of 142)



 
It's the next field.

/k1
 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 10:07 AM (reply 29 of 142)



 
maybe

haha
 
This topic has 142 replies on 10 pages.    « Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next »