Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 17th, 2007, 04:13 PM
kolorowy kolorowy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 27 kolorowy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 37 m 12 sec
Reputation Power: 0
Recursive linked list

Hello

I need some help with reversing single linked list, so that the ordering of the nodes becomes opposite.

I wrote it this way but need to use recursion

???

Code:
public void reverse() {
    Node current = head;
    head = null;
    while (current != null) {
        Node temp = current;
        current = current.next;
        temp.next = head;
        head = temp;
    }
}

Reply With Quote
  #2  
Old May 6th, 2007, 10:59 PM
crispin1396 crispin1396 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 1 crispin1396 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 9 sec
Reputation Power: 0
Solution??

Did you ever find out how to write it with recursion?
I'm afraid that recursion is not my strong suit...
temp.next = head;
head = temp;

Quote:
Originally Posted by kolorowy
Hello

I need some help with reversing single linked list, so that the ordering of the nodes becomes opposite.

I wrote it this way but need to use recursion

???

Code:
public void reverse() {
    Node current = head;
    head = null;
    while (current != null) {
        Node temp = current;
        current = current.next;


    }
}

Reply With Quote
  #3  
Old May 10th, 2007, 07:35 AM
jad939933 jad939933 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 1 jad939933 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 34 sec
Reputation Power: 0
Thumbs up

hi dudes,
the only clean way to do it to create a new LinkedList and to fill it from the initial LinikedList from end node to start node.

ex :

public LinkedList getReverse() {
LinkList newList=new LinkedList();
Node current = head;
head = null;

Node newListCurrent=newList.head;

while (current != null) {
Node temp = current;

current.next=newListCurrent;
newListCurrent=current;

current=current.next;

}
return newList; // something like that
}

jad939933, (URL address blocked: See forum rules)/index.php?page_id=17

Last edited by jad939933 : May 10th, 2007 at 08:13 AM. Reason: code

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Recursive linked list


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT