Skip to content

Instantly share code, notes, and snippets.

@erjan
Last active August 29, 2015 14:04
Show Gist options
  • Save erjan/1d441b59675afb68221f to your computer and use it in GitHub Desktop.
Save erjan/1d441b59675afb68221f to your computer and use it in GitHub Desktop.
node testing
public class node {
int value ;
node next ;
public node(int newvalue){
value = newvalue ;
next = null ;
}
public node(int newvalue, node n){
value = newvalue ;
next = n ;
}
public static void print_list(node printMe){
while( printMe != null){
if( printMe.next == null){
System.out.println(printMe.value) ;
break ;
}
System.out.print(printMe.value + "->") ;
printMe = printMe.next ;
}
System.out.println() ;
}
public static node reverse( node head, node x){
if(head == null) return x ;
node temp = head ;
if( head.next != null){
head = head.next ;
x = head ;
x.next = temp ;
reverse(head,x) ;
}
else return x ;
return null ;
}
public static void main(String[] args) {
/*
node first = new node(1) ;
node second = new node(2) ;
first.next = second ;
node third = new node(3) ;
second.next = third ;
node fourth = new node(4) ;
third.next = fourth ;
node fifth = new node(5) ;
fourth.next = fifth ;
node sixth = new node(6) ;
fifth.next = sixth ;
node seventh = new node(7) ;
sixth.next = seventh ;
*/
node rt = new node(1, new node(2, new node(3))) ;
print_list(rt) ;
//print_list(first) ;
//node result = reverse(first, null) ;
//print_list(result) ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment