翻转链表
import java.util.ArrayList;
public class JZ3 {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if (listNode == null){
return new ArrayList<>();
}
if (listNode.next == null){
return new ArrayList<>(listNode.val);
}
ListNode node = new ListNode(-1);
ArrayList<Integer> res = new ArrayList<>();
ListNode p = listNode;
while (p != null){
ListNode temp = p;
p = p.next;
temp.next = node.next;
node.next = temp;
}
p = node.next;
while (p != null){
res.add(p.val);
p = p.next;
}
return res;
}
}
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}

京公网安备 11010502036488号