|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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;
}
}
|
|
#2
|
|||
|
|||
|
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:
|
|
#3
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Recursive linked list |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|