思路:链表反转。
public class Main4 {
public static class ListNode{
int val;
ListNode next;
public ListNode(int x){
this.val = x;
}
}
public static ListNode reverse(ListNode head){
if(head==null || head.next==null){
return head;
}
ListNode newHead = reverse(head.next);
head.next.next = head;
head.next= null;
return newHead;
}
public static void print(ListNode head){
while(head!=null){
System.out.println(head.val+" ");
head = head.next;
}
}
}