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.    1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next »

Posts:357
Registered: 8/5/04
Reversing LinkedList elements  
Aug 8, 2004 8:04 AM



 
Okay - now reading and trying to figure out the following code is like doing mumbo-jumbo...

if anyone can figure out what it exactly does can that person please explain...

following code is a method in LinkedList class...

What I want to do is to reverse the list i.e. make the last element first and the first element last.

thanks

 public void reverse()   {       if (first == null) return;      Link previous = first;      Link current = first.next;      first.next = null;      while (current != null)      {          Link next = current.next;         current.next = previous;         previous = current;         current = next;      }      first = previous;   }
 

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



 
Hi,

There is an easier way...
    Collections.reverse(list);


You can also look at the source code for Collections.reverse if you want to see how it is done.

/Kaj
 

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



 
OK -

You're getting misled by naming again A.

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

Stop here. Think about the above.

Suppose we just changed the names of previous and next to next and previous.

What difference would there be?

Stop here. Think about the above.

So there could be no possible reason to "reverse" a double linked list.

Could there?

/k1
 

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



 
What I want to do is to reverse the list i.e. make the
last element first and the first element last.

If you have a single linked list this requires a restructuring. You can for example first traverse the list and store each node in an array, then read the array in reversed order and at the same time build the list anew.
 

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



 
What I want to do is to reverse the list i.e. make
the
last element first and the first element last.

If you have a single linked list this requires a
restructuring. You can for example first traverse the
list and store each node in an array, then read the
array in reversed order and at the same time build the
list anew.

I have to reverse elements without using iterator.
 

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



 
Another method is to traverse the singly linked list and at the same time add each node at the very beginning of a new linked list.
 

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



 
It's a double linked list.
/k1
 

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



 
I don't want anyother methods folks I just want to understand someone elses code that I just posted,
maybe if anyone can comment each line of the code and specify what it is doing i'll appreciate it!
if you need whole code please let me know

thanks
 

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



 
I don't want anyother methods folks I just want to
understand someone elses code that I just posted,
maybe if anyone can comment each line of the code and
specify what it is doing i'll appreciate it!
if you need whole code please let me know

thanks

Do you know Catch 22?

There's a nice little quote in there "You are a very strange person, Yossarian".

/k1
 

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



 


Do you know Catch 22?

Whats that please don't waste my time with this!? ok?

unless ofcourse u've got something useful to say
 

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



 
It was just educational. Please don't get angry.

How's the Regex problem doing?

/k1
 

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



 
its done

case '*' means matching of two strings

i.e. if i input two string like this:

*is this

it Will return true

for case '?' it will allow only one character to be not matching
i.e.

th$s this

will return True
 

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



 
Congratulations.

That code really sucks though, don't you agree?

/k1
 

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



 


That code really sucks though, don't you agree?

not really only the case '*' is a lil bit crazy.

domatch(p.substring(1), v); domatch(p, v.substring(1));

I don't get it what is it doing with it?

default and case '?' are pretty good.

//can you ans. the OP question?
 

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



 


That code really sucks though, don't you agree?

not really only the case '*' is a lil bit crazy.

 domatch(p.substring(1), v); domatch(p, v.substring(1));

I don't get it what is it doing with it?

default and case '?' are pretty good.

Your sense of design and mine differ considerably. But that's ok, I can live with that.

//can you ans. the OP question?

I did. You just didn't read the answer I gave.

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