题解 | #从尾到头打印链表#
从尾到头打印链表
https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
import java.util.*;
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ListNode pre = null;
ListNode curr = listNode;
ListNode tmp = null;
while(curr != null){
tmp = curr.next;
curr.next = pre;
pre = curr;
curr = tmp;
}
ArrayList<Integer> res = new ArrayList<>();
while(pre != null){
res.add(pre.val);
pre = pre.next;
}
return res;
}
}
#剑指offer##java##算法笔试#剑指Offer2-Java题解 文章被收录于专栏
剑指offer题解(java版)
查看16道真题和解析