1.非递归
class ListNode{
String airport;
ListNode link;
}
class LinkedNode{
int length;
ListNode firstNode;
public void reverse(){
ListNode R,N,L1;
L1 = firstNode;
R = null;
while(L1 != null){
N = L1;
L1 = L1.link;
N.link = R;
R = N;
}
firstNode = R;
}
}
递归方法:
class ListNode{
String airport;
ListNode link;
}
class linkedNode{
int length;
ListNode firstNode;
void reverse(){
firstNode = reverse1(firstNode);
}
ListNode reverse1(ListNode L){
if(L = null){
return null;
} else {
ListNode head = L;
ListNode tail = L.link;
head.link = null;
return concat(reverse1(tail),head);
}
}
ListNode concat(ListNode L1,ListNode L2){
if(L1 == null){
return L2;
} else {
L1.link = concat(L1.link,L2);
return L1;
}
}
}