时间复杂度O(N)
空间复杂度O(N)
class Solution {
List list = new ArrayList<>();
public int[] reversePrint(ListNode head) {
reverse(head);
int size = list.size();
int[] arr = new int[size];
for(int i = 0;i < size;i++){
arr[i] = list.get(i);
}
return arr;
}
var reversePrint = function(head) {
if(head == null) return new Array(0);
let count = 0;
let temp = head;
while( temp !== null ) {
count++;
temp = temp.next;
}
let res = new Array(count);
let k = count - 1;
while ( head !== null ) {
res[k--] = head.val;
head = head.next;
}
return res;
};
public int[] reversePrint(ListNode head) {
if (head null) return new int[0];
ListNode a = head;
int length = 0;
while (head != null){
head = head.next;
length++;
}
int[] array = new int[length];
for(int i = length – 1; i >= 0; i–){
if (a != null){
array[i] = a.val;
a = a.next;
}
}
return array;
}
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
if not head:
return []
count=0
cur=head
while cur:
count+=1
cur=cur.next
res=[0]*count
while head:
count-=1
res[count]=head.val
head=head.next
return res
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
if not head:
return []
# 累计链表的长度
count = 0
temp = head
while temp:
count += 1
temp = temp.next
# 创建结果数组,倒着输入链表元素
res = [0] * count
k = count - 1
while head:
res[k] = head.val
k -= 1
head = head.next
return res
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
# res = []
# while head:
# res.insert(0, head.val)
# head = head.next
# return res
res = []
while head:
res += [head.val]
head = head.next
return res[::-1]
class Solution {
public int[] reversePrint(ListNode head) {
//用于存放链表值的集合
ArrayList list = new ArrayList();
while(head != null){
list.add(head.val);
head = head.next;
}
Collections.reverse(list);
int arr[] = new int[list.size()];
for(int i = 0; i < list.size(); i++){
arr[i] = list.get(i);
}
return arr;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
if (head == null) {
return new int[0];
}
Stack stack = new Stack<>();
ListNode cur = head;
int n = 0;
while(cur != null) {
stack.push(cur.val);
cur = cur.next;
n++;
}
int[] result = new int[n];
for(int i = 0; i < n; i++) {
result[i] = stack.pop();
}
return result;
}
}
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 ;
}
}
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
if not head:
return []
res = self.reversePrint(head.next)
res.append(head.val)
return res
共以下 82 个回答
“`java a = new ArrayList<>();
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
ArrayList
public int[] reversePrint(ListNode head) {
recur(head);
int[] res = new int[a.size()];
for(int i=0;i
void recur(ListNode head){ if(head==null){ return ; } recur(head.next); a.add(head.val); }}
递归和Java ArrayList
时间复杂度O(N) list = new ArrayList<>();
空间复杂度O(N)
class Solution {
List
public int[] reversePrint(ListNode head) {
reverse(head);
int size = list.size();
int[] arr = new int[size];
for(int i = 0;i < size;i++){
arr[i] = list.get(i);
}
return arr;
}
}
时间:O(n)
空间:O(1)
class Solution {
public int[] reversePrint(ListNode head) {
if(head null){
return new int[0];
}
int len = 0;
ListNode tmp = head;
while(tmp != null){
len++;
tmp = tmp.next;
}
int[] arr = new int[len];
for(int i = len-1 ; head != null ;i–){
arr[i] = head.val;
head = head.next;
}
return arr;
}
时间复杂度O(n)
空间复杂度O(1)
方法:递归
时间复杂度:O(n)
空间复杂度:O(n)
思路:原地反转,空间复杂度O(1),从右往左遍历
时间复杂度:O(n)
空间复杂度:O(n)
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reversePrint(head *ListNode) []int {
if head nil {
return []int{}
}
//1,3,2
subRes := reversePrint(head.Next)
}
先用递归进入到链表尾部,然后通过一个全局的ArrayList来进行数据的保存,最后将其转化为数组进行输出
public int[] reversePrint(ListNode head) {
if (head null) return new int[0];
ListNode a = head;
int length = 0;
while (head != null){
head = head.next;
length++;
}
int[] array = new int[length];
for(int i = length – 1; i >= 0; i–){
if (a != null){
array[i] = a.val;
a = a.next;
}
}
return array;
}
时间:O(n)需要遍历数组。空间:O(n)新建了数组和指针
代码:
时间复杂度:遍历两次链表,用时为2*n,为O(n)
空间复杂度:O(1),未使用额外存储空间
““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];
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;
}
}
时间复杂度0(n)
空间复杂度0(1)
时间复杂度O(n)
空间复杂度O(n)
遍历链表得到长度,创建等长数组
再次遍历倒序输入数组
方法一:递归
Time:O(n)
Space:O(n)
方法二:从右到左进行遍历
Time:O(n)
Space:O(1)
堆栈
创建一个新数组res,倒着向数组中填入链表元素值
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)。
空间复杂度:O(1)。
解题思路
将链表压入容器当中,可以实现快速的反转。
V.push_back(A->val);
reverse(V.begin(),V.end());
代码
【思路】递归法,内存消耗偏高
【复杂度】时间O(n),空间复杂度O(n)
利用递归实现。时间复杂度:O(n) 空间复杂度:O(n)
打卡
时间复杂度:O(n)
空间复杂度:O(n)
“` C++ reversePrint(ListNode* head) { ans;
注意可能给一个元素的列表,所以一开始定义head->next的时候要特判是不是nullptr
class Solution {
public:
vector
ListNode* cur = head;
vector
if(head == nullptr) return ans;
ListNode *pre1 = head -> next;
while(pre1 != nullptr){
ListNode* pre2 = nullptr;
if(pre1 -> next !=nullptr) pre2 = pre1->next;
pre1->next = cur;
cur = pre1;
pre1 = pre2;
}
head->next = nullptr;
while(cur != nullptr){
ans.push_back(cur->val);
cur = cur ->next;
}
return ans;
}
};
“`
数组反向填充
“`C#
public class Solution {
public int[] ReversePrint(ListNode head) {
}
“`
翻转数组实现
时间复杂度O(n)空间O(1)
class Solution { stack = new Stack ();
public int[] reversePrint(ListNode head) {
Stack
ListNode temp = head;
}
时间复杂度:O(n),空间复杂度O(n)
“`C
int* reversePrint(struct ListNode* head, int* returnSize){
if(head == NULL){
*returnSize=0;
return NULL;
}
}
时间复杂度是O(n)
空间复杂度是O(n)
python切片
“`c++ reversePrint(ListNode* head) { ans;
// c++实现
class Solution {
public:
vector
vector
while(head){
ans.insert(ans.begin(),head->val);
head = head->next;
}
return ans;
}
};
时间复杂度O(n)
cpp实现& result) reversePrint(ListNode* head) { result;
void returnresult(ListNode* cur,vector
{
if(cur->next!=NULL)
{
returnresult(cur->next,result);
}
result.push_back(cur->val);
}
vector
vector
ListNode* cur = head;
if(curNULL)return result;
returnresult(cur,result);
return result;
}
利用栈
利用逆向填充数组
利用栈的先入后出特性,将链表节点值依次放入栈中,再依次出栈存入数组
时间复杂度O(N)
空间复杂度O(N)
时间复杂度 O(n) 空间复杂度 O(n)
时间复杂度:O(n)
空间复杂度:O(n)
递归
时间:O(n)
空间:O(n)
时间复杂度O(n)
C++ 两种方法:
1. 递归
TC: O(n)
SC: O(n)
“`c++res; reversePrint(ListNode* head) {
class Solution {
vector
public:
vector
printReverse(head);
return res;
}
void printReverse(ListNode* curr) {
if (!curr) {
return;
}
printReverse(curr->next);
res.push_back(curr->val);
}
};
【时间O(n),空间O(n)】
依次访问并插入vector中
翻转vector
时间复杂度:O(n)
空间复杂度:O(n)
时间复杂度:O(n)
空间复杂度:O(1)
1、先判空
2、计算待反转链表的长度
3、new一个等长的数组,将链表从头开始放在数组的尾部,往前填充
时间复杂度O(n);
空间复杂度O(n);
思路:递归——借鉴树的后序遍历,先不断递归到下一个节点,当遇到空指针才会结束递归,依次弹出之前递归栈中的东西,以此作为翻转的结果
时间复杂度:O(n), 空间复杂度:O(n)
思路:先遍历一遍链表计算长度,再遍历一遍链表反向填入数组中
时间复杂度O(n),空间复杂度O(1)
/** reversePrint(ListNode head) { result;
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector
vector
if(!head) return result;
ListNode* cur = head;
ListNode* pre = nullptr;
while(cur){
ListNode *node = cur->next;
cur->next = pre;
pre = cur;
cur = node;
}
while(pre){
result.push_back(pre->val);
pre = pre->next;
}
return result;
}
};
可以
简单
C++:原地反转链表+顺序遍历 reversePrint(ListNode* head) { vec(0);
class Solution {
public:
vector
vector
if(head NULL) return vec;
ListNode* cur = head;
ListNode* tmp;
ListNode* pre = NULL;
while(cur){
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
while(pre){
vec.push_back(pre->val);
pre = pre->next;
}
return vec;
}
};
反转链表多加了一项将节点内存储的数据插入数组中
C++
执行用时:4 ms
内存消耗:11.3 MB
从尾到头打印链表:
使用递归完成,由于需要使用数组返回,所以新建一个vector数组,如果该节点的下一个节点没有指向NULL的话,继续调用该函数,当条件不符时,就将该节点的值放入数组中,最后返回该数组即可。
时间复杂度O(n),空间复杂度O(n)
单链表只能从前往后遍历,不支持从后往前遍历,这时候选择栈来辅助,也可以选择用递归。这里用递归
方法一: reversePrint(ListNode* head) { subprint = reversePrint(head->next); res; reversePrint(ListNode* head) { nullptr){ res; reversePrint(ListNode* head) { res; cur = stack.top();
递归
class Solution {
public:
vector
//base case
if(head nullptr){
return {};
}
//把子链表翻转结果算出来
vector
//加上头部指针指向的值
subprint.push_back(head->val);
return subprint;
}
};
class Solution {
vector
int len = 0;
int p = 0;
public:
vector
traverse(head);
return res;
}
void traverse(ListNode* head){
if(head
// 到达链表尾部,此时知道了链表的总长度
// 创建结果数组
res.resize(len);
return;
}
len++;
traverse(head->next);
//后序位置,可以倒序操作链表
res[p] = head->val;
p++;
}
};
栈方法
class Solution {
vector
int len = 0;
int p = 0;
public:
vector
stack<ListNode> stack;
vector
while(head){
stack.push(head);
head = head->next;
}
while(!stack.empty()){
ListNode
stack.pop();
res.push_back(cur->val);
}
return res;
}
};
c实现
int* reversePrint(struct ListNode* head, int* returnSize){ if(!head){ returnSize=0; return malloc(sizeof(int)10000); } int * ans=reversePrint(head->next,returnSize); ans[(*returnSize)++]=head->val; return ans;}
思路:
s1. 通过递归将Node值存到List里;
s2. 将list转化为数组。
时间复杂度:O(n)
空间复杂度:O(n)
C++实现
思路:
采用递归的方法,递归的终止条件是只要递归不为空,递归的顺序就是和采用入栈的顺序是一样的。
“`c++ reversePrint(ListNode* head) { NewStack = reversePrint(head->next);
class Solution {
public:
vector
if(!head){
return {};
}
vector
NewStack.push_back(head->val);
return NewStack;
}
};
1.用栈来实现 stack=new ArrayDeque<>();
class Solution {
public int[] reversePrint(ListNode head) {
if(headnull){
return new int[0];
}
Deque
int size=0;
while(head!=null){
stack.push(head);
head=head.next;
size++;
}
}
2.递归实现
class Solution {
public int[] reversePrint(ListNode head) {
if(headnull){
return new int[0];
}
}
真的会谢,忘得一干二净
从左向右遍历原数组,在新数组中从右向左进行存储
时间复杂度:O(n),空间复杂度:O(1)
java实现
时间复杂度:n,空间复杂度为n。
思路:第一种解法是帅地的逆序,第二种是使用递归的方法,并结合list。
时间复杂度O(n),空间复杂度O(n)
递归算法
思路
代码
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
if not head:
return []
temp=head
count=0
while temp:
count+=1
temp=temp.next
res=[0]count
k=count-1
while head:
res[k]=head.val
k-=1
head=head.next
return res
”’
1判断这个链表是否为空,也就是头结点head是否为空,如果为空,直接返回[]
2添加一个指针temp,指向head,另外设置一个计数count,初始化为0
3判断指针是否为空,也就是指针有没有对象,用while表循环,在这个范围内,如果指针不为空,也就是指着对象,用count+1表示增加一个数,然后指针往后面走一步,直到指针为空,用count统计对象数
4接下来创建一个新的列表,用[0]count表示一个列表中有count个0元素
5建立一个索引k,也就是等于count-1
6然后 索引不断往前走,head往后走,这样就把内容倒着输入进去了
7返回res列表
时间复杂度:O(n) 额外空间复杂度:O(1)
这里是Ming的打卡:
思路1:遍历链表将值存入集合,再将集合反转,把值存入数组
思路2:反正会将链表遍历一遍,得到他的长度再建数组,从后往前给数组赋值
public int[] reversePrint(ListNode head){
if(headnull){
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;
}
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度O(n),空间复杂度O(n)