头插法
class Node {
int value;
Node next;
@Override
public String toString ( ) {
return "Node{" +
"value=" + value +
", next=" + next +
'}' ;
}
}
public class Test {
public static void main ( String[ ] args) {
int arr[ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;
Node link = null;
for ( int i = 0 ; i < arr. length; i++ ) {
Node x = new Node ( ) ;
x. value = arr[ i] ;
x. next = link;
link = x;
}
System. out. println ( link. toString ( ) ) ;
}
}
尾插法
class Node {
int value;
Node next;
@Override
public String toString ( ) {
return "Node{" +
"value=" + value +
", next=" + next +
'}' ;
}
}
public class Test {
public static void main ( String[ ] args) {
int arr[ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;
Node link = new Node ( ) ;
link. value = arr[ 0 ] ;
Node flag = link;
for ( int i = 0 ; i < arr. length; i++ ) {
Node x = new Node ( ) ;
x. value = arr[ i] ;
flag. next = x;
flag = flag. next;
}
System. out. println ( link. toString ( ) ) ;
}
}