/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
ArrayList<Integer> al = new ArrayList<Integer>();
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if(listNode != null)//注意是if而不是while!因为递归就相当于一个while!!!!
{
printListFromTailToHead(listNode.next);
al.add(listNode.val);
}
return al;
}
}
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode curNode = head;
ListNode preNode = null;
ListNode nextNode = null;
if(head == null){
return head;
}
while(curNode != null){
nextNode = curNode.next;
curNode.next = preNode;
preNode = curNode;
curNode = nextNode;
}
return preNode;//preNode才是表头
}
}