Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reverse the nodes in linklist

843785Nov 6 2008 — edited Nov 7 2008
import java.io.*;
import java.util.*;


class Node{
	int num;
	Node next;
	
	Node(int n){
		num = n;
		next = null;
	}
}

class RecursivePrint{
	public static void main(String[] args){
	
		Node np, last;
		Node top = null;
		last = null;
		for(int i = 1; i <11; i++){
			np = new Node(i);
			if(top==null)top = np;
				else
			last.next = np;
			last = np;
		}
		
		
	
		recursiveprint(top);
		
	}//end main

	
	
	public static Node recursiveprint(Node top){
		*Node newtop = null;*
*		Node temp = null;*
*		*
*		if(top==null){*
*			return null;*
*			*
*		}else{*
*			temp = top;*
*			top = top.next;*
*			temp.next = newtop;*
*			newtop = temp;*
*			System.out.println(newtop.num);*
*			return recursiveprint(newtop.next);*		}
	}//end recursive print
}//end class	
in my reverse method when I try to reverse it only print 1 which indicates that it has stated the process and unable to complete. I try a while loop but it ran many times resulting in null output.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 5 2008
Added on Nov 6 2008
3 comments
98 views