题目来源:
leetcode题目,网址:LCR 123. 图书整理 I - 力扣(LeetCode)
解题思路:
遍历链表获得图书的正序,然后倒序遍历即可。
解题代码:
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode() {}
- * ListNode(int val) { this.val = val; }
- * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
- * }
- */
- class Solution {
- public int[] reverseBookList(ListNode head) {
- List<Integer> list=new ArrayList<>();
- while(head!=null){
- list.add(head.val);
- head=head.next;
- }
- int[] res=new int[list.size()];
- for(int i=0;i<res.length;i++){
- res[res.length-i-1]=list.get(i);
- }
- return res;
- }
- }
总结:
无官方题解。
用栈可能更好一点。