class Solution {
public int[] reversePrint(ListNode head) {
Deque<Integer> stack = new ArrayDeque<>();
while (head != null) {
stack.push(head.val);
head = head.next;
}
int[] res = new int[stack.size()];
int i = 0;
while (!stack.isEmpty()) {
res[i++] = stack.pop();
}
return res;
}
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reversePrint(head *ListNode) []int {
if head == nil {
return []int{}
}
res := reversePrint(head.Next)
res = append(res, head.Val)
return res
}
”’java
class Solution {
public int[] reversePrint(ListNode head) {
List list = new ArrayList<>();
while(head != null){
list.add(head.val);
head = head.next;
}
int[] re = new int[list.size()];
for(int i = 0;i < list.size();i++){
re[i] = list.get(list.size()-1-i);
}
return re;
}
}
”’
时间复杂度O(n)
空间复杂度O(n)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
if(head==null) return new int[]{};
List<Integer> list = new ArrayList<>();
ListNode p = head;
while(p!=null){
list.add(p.val);
p=p.next;
}
int n = list.size();
int[] res = new int[n];
for(int i =0;i<n;i++){
res[i] = list.get(n-i-1);
}
return res;
}
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reversePrint(head *ListNode) []int {
var ret []int
var pre, cur *ListNode = nil, head
for cur != nil {
next := cur.Next
cur.Next = pre
pre = cur
cur = next
}
for pre != nil {
ret = append(ret, pre.Val)
pre = pre.Next
}
return ret
}
//建一个stack
/*为什么在第二个for循环中不能使用for(int i = 0; i < stack.size(); i++)而要使用for(int i = 0; i < res.length; i++)。
这是因为在每次循环中,我们都会从栈中弹出元素,并将其存储在数组res中。在每次循环之后,栈的大小会减少,但是我们需要对数组res进行正确的遍历。
如果我们使用for(int i = 0; i < stack.size(); i++)作为循环条件,那么在每次循环中,都会重新计算栈的大小。由于每次循环栈的大小都在减小,循环条件就会变得不准确,导致可能会漏掉一些元素或者访问到不存在的索引位置,从而导致错误。
相反,如果我们使用for(int i = 0; i < res.length; i++)作为循环条件,由于数组res的大小是提前确定的,并且与栈的大小一致,所以循环条件始终准确无误。这样可以确保我们能够正确地遍历整个数组并获取所有栈中的元素。
*/
class Solution {
public int[] reversePrint(ListNode head) {
LinkedList<Integer> stack = new LinkedList<>();
while(head != null){
stack.addLast(head.val);
head = head.next;
}
int[] res = new int[stack.size()];
for(int i = 0; i < res.length; i ++){
res[i] = stack.removeLast();
}
return res;
}
}
共以下 64 个回答
打卡
栈:将链表元素压入栈中,获取站的长度,定义数组,依次取出栈中元素;通过Stack实现 stack=new Stack();
递归:链表先走到头,判断当前节点为空时,获得元素个数,new数组;开始返回,数组添加元素,每返回一层index++,用于构建下标
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
// class Solution {
// public int[] reversePrint(ListNode head) {
// Stack
// ListNode cur=head;
// while(cur!=null){
// stack.push(cur);
// cur=cur.next;
// }
// int size=stack.size();
// int[] result=new int[size];
// for(int i=0;i<size;i++){
// result[i]=stack.pop().val;
// }
// return result;
// }
// }
class Solution {
int[] arr=null;
int length=0;
int index=0;
public int[] reversePrint(ListNode head) {
reverse(head);
}
先记录链表值,再逆序数组
递归
时间复杂度O(n)空间复杂度O(n)递归+循环
打卡: reverse; reversePrint(ListNode* head) {
class Solution {
public:
vector
vector
};
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
//三种 一个用栈 一个用遍历头差法 递归
ListNode pre =new ListNode();
pre.next=null;
ListNode temp;
int n =0;
while(head!=null)
{
temp=head;
head=head.next;
temp.next =null;
temp.next=pre.next;
pre.next=temp;
n++;
}
int result[]=new int[n];
n=0;
temp=pre.next;
while(temp!=null)
{
result[n++]=temp.val;
temp=temp.next;
}
return result;
}
}
代码如下
代码
从尾到头打印练笔哦
递归法
代码如下
“`java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
if(head == null)
return new int[0];//链表为空返回空数组
}
public int[] reversePrint(ListNode head) {
if (head null) {
return new int[0]; // 如果链表为空,返回一个空数组
}
打卡
打卡:
class linkelist:
代码
时间复杂度:O(n)
空间复杂度:O(n)
时间复杂度:O(n)
空间复杂度:O(n)
时间复杂度:O(n)
空间复杂度:O(1)
“`c++ reversePrint(ListNode* head) { a(count);
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector
ListNode *p;
p=head;
int count=0;
while(p!=NULL)
{
count++;
p=p->next;
}
vector
int i;
p=head;
for(i=count-1;i>=0;i–)
{
a[i]=p->val;
p=p->next;
}
return a;
}
};
“`
二
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
if (head null) {
return new int[0];
}
int count = 0;
ListNode temp = head;
while (temp != null) {
count++;
temp = temp.next;
}
int[] res = new int[count];
int k = count – 1;
while (head != null) {
res[k–] = head.val;
head = head.next;
}
return res;
}
}
”’cpp reversePrint(ListNode* head){ stk; ans;
//栈模拟递归
class Solution {
public:
vector
stack
vector
};
”’
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
list=[]
while head:
list.append(head.val)
head=head.next
return list[::-1]
列表倒序输出
用stack和cpp vector特性
思路如下:
1. 先遍历,统计链表长度size
2. 创建长度为size的数组
3. 将链表元素从尾到头存入,由于单链表元素不能从尾至头遍历,则考虑将单链表从头开始遍历,然后数组从尾至头存储元素。
时间复杂度:O(n)
空间复杂度:O(1)
class Solution {
public int[] reversePrint(ListNode head) {
if(head null) return new int [0];
ListNode p = head;
int count= 0;
while(p!=null){
count++;
p=p.next;
}
int[] x = new int[count];
for(int i=count-1;i>=0;i–){
x[i]=head.val;
head=head.next;
}
return x;
}
}
时间复杂度 O(n)
空间复杂度O(n)
从尾到头打印链表,第一遍遍历链表获得长度以创建数组,第二遍存入数组。 时间O(n) 空间O(n)
利用栈 stack = new Stack ();
时间复杂度:on
空间复杂度:on
class Solution {
public int[] reversePrint(ListNode head) {
Stack
ListNode t = head;
while(t != null) {
stack.push(t);
t = t.next;
}
int size = stack.size();
int[] res = new int[size];
for (int i = 0; i < size; i++) {
res[i] = stack.pop().val;
}
return res;
}
}
简单题刷起来还是挺爽的
/** reversePrint(ListNode head) {
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)
class Solution {
public:
vector
if (!head) {
return {};
}
};
先写一个reverse函数,用双指针来进行链表的反转。然后将反转后的链表按顺序输出。
C++ 代码rec; reversePrint(ListNode* head)
class Solution {
public:
vector
vector
{
while (head!=NULL)
{
rec.push_back(head->val);
head=head->next;
}
reverse(rec.begin(), rec.end());
return rec;
}
};
时间复杂度:O(n)
空间复杂度:O(n)
创建数组,从右往左填充数组
时间复杂度:O(n)
空间复杂度:O(n)
代码如下:
时间复杂度:O(n)
空间复杂度:O(n)
”’java list = new ArrayList<>();
class Solution {
public int[] reversePrint(ListNode head) {
List
while(head != null){
list.add(head.val);
head = head.next;
}
int[] re = new int[list.size()];
for(int i = 0;i < list.size();i++){
re[i] = list.get(list.size()-1-i);
}
return re;
}
}
”’
时间复杂度O(n)
空间复杂度O(n)
”C++ reversePrint(ListNode* head) { ans(count);
class Solution {
public:
vector
if(headNULL)return {};
int count=0;
ListNode* temp=head;
for(int i=0;temp!=NULL;i++){count++;temp=temp->next;}
vector
while(head)
{
ans[–count]=head->val;
head=head->next;
}
};
”
内存消耗:
8.3 MB
Definition for singly-linked list.
class ListNode(object):
def init(self, x):
self.val = x
self.next = None
#递归
class Solution(object):
def reversePrint(self, head):
“””
:type head: ListNode
:rtype: List[int]
“””
return self.reversePrint(head=head.next) + [head.val] if head else []
#栈
class Solution(object):
def reversePrint(self, head):
stack=[]
while head:
stack.append(head.val)
head=head.next
return stack[::-1]
class Solution(object):
def reversePrint(self, head):
stack=[]
while head:
stack.append(head.val)
head=head.next
#逆向赋值
class Solution(object):
def reversePrint(stack, head):
len=0
temp=head
while temp:
len+=1
temp=temp.next
递归
时间复杂度:O(n)
空间复杂度:O(n)
时间复杂度:O(n)
空间复杂度:O(n)
使用反向迭代器效率会高些c++ reversePrint(ListNode* head) { ans(s);
”’
class Solution {
public:
vector
ListNode* dnode=head;
int s=0;
while(dnode!=NULL){
s++;
dnode=dnode->next;
}
vector
dnode=head;
for(auto i=ans.rbegin();i!=ans.rend();i++){
*i =dnode->val;
dnode=dnode->next;
}
return ans;
};
”’
时间复杂度:O(2n)
空间复杂度:O(2n)
时间复杂度On
//建一个stack
/*为什么在第二个for循环中不能使用for(int i = 0; i < stack.size(); i++)而要使用for(int i = 0; i < res.length; i++)。
这是因为在每次循环中,我们都会从栈中弹出元素,并将其存储在数组res中。在每次循环之后,栈的大小会减少,但是我们需要对数组res进行正确的遍历。
如果我们使用for(int i = 0; i < stack.size(); i++)作为循环条件,那么在每次循环中,都会重新计算栈的大小。由于每次循环栈的大小都在减小,循环条件就会变得不准确,导致可能会漏掉一些元素或者访问到不存在的索引位置,从而导致错误。
相反,如果我们使用for(int i = 0; i < res.length; i++)作为循环条件,由于数组res的大小是提前确定的,并且与栈的大小一致,所以循环条件始终准确无误。这样可以确保我们能够正确地遍历整个数组并获取所有栈中的元素。
*/
1.递归
时间:O(n)
空间:O(n)
时间:O(n)
空间:O(1)