// 答案
public static int[] reverseBookList(ListNode head) {
Stack stack = new Stack<>();
int len = 0;
ListNode p = head;
while (p != null){
stack.push(p.val);
p = p.next;
len++;
}
int[] result = new int[len];
int index = 0;
while (!stack.isEmpty()){
result[index++] = stack.pop();
}
return result;
}
class Solution {
public int[] reverseBookList(ListNode head) {
Stack stack = new Stack<>();
ListNode cur = head;
while(cur!=null){
stack.push(cur);
cur=cur.next;
}
int s = stack.size();
int[] num = new int[s];
for(int i=0;i<s;i++){
num[i]=stack.pop().val;
}
“`java
class Solution {
public int[] reverseBookList(ListNode head) {
Stack stack = new Stack<>();
ListNode p = head;
while (p != null) {
stack.push(p.val);
p = p.next;
}
int[] ans = new int[stack.size()];
int index = 0;
while (!stack.isEmpty()) {
ans[index++] = stack.pop();
}
return ans;
}
}
//倒着来就行
class Solution {
public int[] reverseBookList(ListNode head) {
int l = 0;
ListNode p = head;
while(p!=null){
l++;
p = p.next;
}
int[] t = new int[l];
p = head;
for(int i = l-1 ; i >=0 ; i-- ){
t[i] = p.val;
p = p.next;
}
return t;
}
}
public int[] reverseBookList(ListNode head) {
head = reverseNodeList(head);
List<Integer> res = new ArrayList<>();
ListNode p = head;
while(p != null){
res.add(p.val);
p = p.next;
}
int[] r = new int[res.size()];
for(int i = 0; i < res.size(); i++) r[i] = res.get(i);
return r;
}
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
temp = head
cnt = 0
while temp:
temp = temp.next
cnt += 1
ans = [0]*cnt
i = -1
while head:
ans[i] = head.val
head = head.next
i -= 1
return ans
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
stack = []
temp = head
while temp != None:
stack.append(temp.val)
temp = temp.next
result = []
for i in range(len(stack)):
result.append(stack.pop())
return result
time: O(n)
space: O(n)
方法2:正向走一遍源链表获取长度 然后再走一遍链表并反向塞入结果数组
这两种方法都是要遍历两遍。
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
count = 0
temp = head
# count the length of the list
while temp is not None:
count += 1
temp = temp.next
result = [0] * count
temp = head
# fill the list in reverse
for i in range(count):
result[count - i - 1] = temp.val
temp = temp.next
return result
中间的for也可以用while 我觉得用for可以搞定index的问题就用了 实际上得别忘了-1因为index是0开始。
time O(n) space O(n)
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
ans = []
while(head):
ans.append(head.val)
head = head.next
ans = ans[::-1]
return ans
时间复杂度O(n) 空间复杂度O(n)
public int[] reverseBookList(ListNode head) {
if(head == null) return new int[0];
ListNode cur = head;
int cnt = 0;
while(cur != null){
cnt++;
cur = cur.next;
}
cur = head;
int i = cnt - 1;
int[] res = new int[cnt];
while(cur != null){
res[i--] = cur.val;
cur = cur.next;
}
return res;
}
class Solution {
public int[] reverseBookList(ListNode head) {
Stack<Integer> stack = new Stack<>();
while(head != null){
stack.push(head.val);
head = head.next;
}
int[] ans = new int[stack.size()];
int i = 0;
while(!stack.isEmpty()){
ans[i] = stack.pop();
i ++;
}
return ans;
}
}
class Solution {
List res = new ArrayList();
public int[] reverseBookList(ListNode head) {
dfs(head);
int[] ans = new int[res.size()];
for(int i = 0; i < ans.length; i++){
ans[i] = res.get(i);
}
return ans;
}
class Solution {
public int[] reverseBookList(ListNode head) {
int len = 0;
ListNode cur = head;
while (cur != null) {
len++;
cur = cur.next;
}
int[] res = new int[len];
cur = head;
for (int i = len - 1; i >= 0; i--) {
res[i] = cur.val;
cur = cur.next;
}
return res;
}
}
遍历链表,记录链表的长度,创建结果数组 res。再次遍历数组,倒序填充元素到数组即可。
也可以第一遍遍历链表时,使用集合用来存储链表节点的值,再倒序填充结果数组即可。
时间复杂度:O(n),遍历了两边链表
空间复杂度:O(n),需要一个大小为 n 的数组用来存储链表节点的值。
共以下 51 个回答
时On^2 空On
class Solution {
public int[] reverseBookList(ListNode head) {
ListNode cnt = head;
int count = 0;
while (cnt != null) {
cnt = cnt.next;
count++;
}
ListNode p = head;
int num = count;
int[] array = new int[count];
for (int j = 0; j < count; j++) {
for(int i = 1; i <= num – 1; i++)
p = p.next;
array[j] = p.val;
num –;
p = head;
}
return array;
}
}
打卡
打卡
打卡
打卡
打卡
// 答案 stack = new Stack<>();
public static int[] reverseBookList(ListNode head) {
Stack
int len = 0;
ListNode p = head;
while (p != null){
stack.push(p.val);
p = p.next;
len++;
}
int[] result = new int[len];
int index = 0;
while (!stack.isEmpty()){
result[index++] = stack.pop();
}
return result;
}
class Solution { stack = new Stack<>();
public int[] reverseBookList(ListNode head) {
Stack
ListNode cur = head;
while(cur!=null){
stack.push(cur);
cur=cur.next;
}
int s = stack.size();
int[] num = new int[s];
for(int i=0;i<s;i++){
num[i]=stack.pop().val;
}
}
利用栈,还是太菜了。时间复杂度O(n);空间复杂度O(n)
题解 (遍历2次,第一次获取链表长度,第二次把值倒序放到数组里面)
“`python
//递归直至节点为空,然后逐层加入节点值进行返回
class Solution(object):
def reverseBookList(self, head):
if head:
return self.reverseBookList(head.next)+[head.val]
else:
return []
时间复杂度:O(N) N为链表长度
空间复杂度:O(N) 系统递归需要使用的栈空间
class Solution {stack=new Stack<>();
public int[] reverseBookList(ListNode head) {
if(headnull) return new int[0];
Stack
while(head!=null){
stack.push(head.val);
head=head.next;
}
int[] ans=new int[stack.size()];
int index=0;
while(!stack.isEmpty()){
ans[index++]=stack.pop();
}
return ans;
}
}
时间:O(n), 空间O(n)
“`
class Solution {
public int[] reverseBookList(ListNode head) {
if (head == null) {
// 链表为空时,返回一个空的数组
return new int[0];
}
}
###
时间复杂度O(n);空间复杂度O(n)
方法1、用栈辅助,利用先进后出的特性,但是时间复杂度和空间复杂度都是O(n)
方法2、反转链表【方法一:递归(O(n));方法二:原地反转,性能好,但容易错】,再顺序遍历存到数组中
方法3、遍历数组改为New一个数组,从右到左遍历,时间复杂度O(n),空间复杂度O(1)
“`java stack = new Stack<>();
class Solution {
public int[] reverseBookList(ListNode head) {
Stack
ListNode p = head;
while (p != null) {
stack.push(p.val);
p = p.next;
}
int[] ans = new int[stack.size()];
int index = 0;
while (!stack.isEmpty()) {
ans[index++] = stack.pop();
}
return ans;
}
}
//倒着来就行
直接翻转链表
/**
* 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 ListNode reverseNodeList(ListNode head){
if(head null || head.next null) return head;
ListNode p = head, q = p.next;
while(q.next != null){
p = p.next;
q = q.next;
}
p.next = null;
q.next = reverseNodeList(head);
return q;
}
}
class Solution {
public int[] reverseBookList(ListNode head) {
int arr[]=new int[10000];
int len=0;
while(head!=null){
arr[len]=head.val;
head=head.next;
len++;
}
int n=len–;
for(int i=0;i<len;i++,len–){
int temp=arr[i];
arr[i]=arr[len];
arr[len]=temp;
}
int result[]=new int[n];
for(int i=0;i<n;i++){
result[i]=arr[i];
}
return result;
}
}//中文
“`c++ reverseBookList(ListNode* head) { res(count);
class Solution {
public:
vector
if(!head)
{
return {};
}
int count = 0;
ListNode* temp = head;
while(temp)
{
count++;
temp = temp->next;
}
vector
int k = count – 1;
while(head)
{
res[k–] = head->val;
head = head->next;
}
return res;
}
};
“`
//从右往左
“java代码
/**
* 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 {
int[] res;
int i=0;
int j=0;
public int[] reverseBookList(ListNode head) {
if(headnull) return new int[0];
solve(head);
return res;
}
}
O(N) O
复杂度 T O(n), M O(n)
“`c++ans; reverseBookList(ListNode* head) {
vector
void dfs(ListNode* head){
if(head==nullptr)return;
dfs(head->next);
ans.push_back(head->val);
return;
}
vector
dfs(head);
return ans;
}
“`
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val=undefined ? 0 : val)
* this.next = (next=undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {number[]}
*/
var reverseBookList = function(head) {
if(head = null) return []
let p0 = head
let res = []
while(p0 ! null) {
res.unshift(p0.val)
p0 = p0.next
}
return res
};
思路:看了下帅地视频的讲解,感觉有种大脑突然开窍的感觉,打破惯性思维从右往左遍历,角度好好,虽然自己写出来的还是这种很普通的
时间空间复杂度:都是O(N)
时间On
空间O1
这就是递归吗
/** reverseBookList(ListNode head) { res;
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
vector
int nCount=0;
vector
for(auto p=head;p!=nullptr;p=p->next)
{
nCount++;
}
res.resize(nCount);
for(auto p=head;p!=nullptr;p=p->next)
{
res[nCount-1]=p->val;
nCount–;
}
return res;
}
};
使用Vector
打卡 res; reverseBookList(ListNode head) {
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
vector
vector
find(head);
return res;
}
void find(ListNode* head){
if(head nullptr){
return;
}
find(head->next);
res.push_back(head->val);
}
};
时间复杂度:O(n)
额外空间复杂度:O(1)
方法1:stack 正向push进一个其他list 然后将其反向pop
time: O(n)
space: O(n)
方法2:正向走一遍源链表获取长度 然后再走一遍链表并反向塞入结果数组
这两种方法都是要遍历两遍。
中间的for也可以用while 我觉得用for可以搞定index的问题就用了 实际上得别忘了-1因为index是0开始。
time O(n) space O(n)
方法3:递归 借用了lc评论区的解法 本质也是利用了递归隐含了栈的特性,最后会从链表最后开始累计结果list。
不得不说这个写法还是很鸡贼的 一遍出可能目前还有点难度 还是朴实的写stack或者反向填充好点。
class Solution { reverseBookList(ListNode* head) { result; l;
public:
vector
vector
stack
while(head){
l.push(head->val);
head = head->next;
}
while(!l.empty()){
result.push_back(l.top());
l.pop();
}
return result;
}
};
思路:结果是倒序输出,正向读链表,并依次入栈,出栈顺序即为结果。
时间复杂度O(n),空间复杂度O(n)。
时间O(n) 空间O(n)
class Solution { tmp=new ArrayList<>(); list){
public int[] reverseBookList(ListNode head) {
List
reverse(head,tmp);
int len=tmp.size();
int[] arr=new int[len];
for(int i=0;i<len;i++){
arr[i]=tmp.get(i);
}
return arr;
}
void reverse(ListNode head,List
if(headnull) return;
reverse(head.next,list);
list.add(head.val);
}
}
时间复杂度O(n)
空间复杂度O(n)
时间O(n),空间O(1)
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
ans = []
while(head):
ans.append(head.val)
head = head.next
ans = ans[::-1]
return ans
时间复杂度O(n) 空间复杂度O(n)
vector reverseBookList(ListNode* head) { v(cnt);
if(head nullptr) return {};
ListNode *p = head, *q = head;
int cnt = 0;
while(p){
p = p->next;
cnt++;
}
vector
while(q){
v[–cnt] = q->val;
q = q->next;
}
return v;
}
时间复杂度 o(n), 空间复杂度o(n)
时间复杂度O(n),空间复杂度O(1)
时间复杂度O(n),空间复杂度O(n)
1.遍历数组直到链表末尾 ans; reverseBookList(ListNode* head) {
2.递归倒序返回,将链表数据添加到数组中
3.返回数组
class Solution {
public:
vector
vector
reverse(head);
return ans;
}
void reverse(ListNode* head){
if(!head) return;
reverse(head->next);
ans.push_back(head->val);
}
};
时间o(n)
空间o(n)
时间o(n)
空间o(1)
//遍历链表,然后用头插法,最后得到的链表是和原来相反的,然后再遍历链表,时间复杂度为O(N),空间为O(1)
/**
* 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) {
int nums = 0;
if(head null) {
int [] arr = new int [0];
return arr;
}
ListNode p1 = new ListNode(-1);
ListNode cur = head;
ListNode nextListNode = head;
//头插法倒序
while (cur != null) {
nextListNode = cur.next;
cur.next = p1.next;
p1.next = cur;
cur = nextListNode;
nums++;
}
}
a.思路讲解
1.将链表元素值放进容器中
2.将容器逆序打印
b.代码
c.时间复杂度o(n),n为链表长度;空间复杂度o(n),使用了额外的容器数组;
递归:
栈 时间 O n 空间On reverseBookList(ListNode* head) {
class Solution {
public:
vector
};
递归
class Solution {
public:
};
思路:使用栈结构
class Solution { res = new ArrayList();
List
public int[] reverseBookList(ListNode head) {
dfs(head);
int[] ans = new int[res.size()];
for(int i = 0; i < ans.length; i++){
ans[i] = res.get(i);
}
return ans;
}
}
思路:递归直到为空再依次返回记录节点值,从而获取到倒序值
时间:O(n)
空间:O(n)
遍历链表,记录链表的长度,创建结果数组
res。再次遍历数组,倒序填充元素到数组即可。也可以第一遍遍历链表时,使用集合用来存储链表节点的值,再倒序填充结果数组即可。
时间复杂度:O(n),遍历了两边链表
空间复杂度:O(n),需要一个大小为
n的数组用来存储链表节点的值。