0%

LCR123

问题描述

书店店员有一张链表形式的书单,每个节点代表一本书,节点中的值表示书的编号。为更方便整理书架,店员需要将书单倒过来排列,就可以从最后一本书开始整理,逐一将书放回到书架上。请倒序返回这个书单链表。

示例 1:

1
2
3
输入:head = [3,6,4,1]

输出:[1,4,6,3]

提示:

1
0 <= 链表长度 <= 10000

解答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 {
List<Integer> tmp = new ArrayList<Integer>();
public int[] reverseBookList(ListNode head) {
reverseBL(head);
int[] res=new int[tmp.size()];
for(int i=0;i<res.length;i++) res[i]=tmp.get(i);
return res;
}
public void reverseBL(ListNode head){
if(head!=null){
reverseBL(head.next);
tmp.add(head.val);
}
}
}