# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseBookList(self, head: Optional[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
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head None:
return []
res = []
cur = head
def reverseList(cur:Optional[ListNode]):
if cur.next None:
res.append(cur.val)
else:
reverseList(cur.next)
res.append(cur.val)
reverseList(cur)
return res
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if not head:
return []
def reversePrint(head):
if head is None:
return []
return reversePrint(head.next) + [head.val]
return reversePrint(head)
“`python
打卡
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if not head:
return []
# 递归
resList = []
cur = self.reverse(head, resList)
return resList
def reverse(self, cur, resList):
if not cur.next:
resList.append(cur.val)
return cur
self.reverse(cur.next, resList)
resList.append(cur.val)
return cur
“`java
class Solution {
List list = new ArrayList<>();
public int[] reverseBookList(ListNode head) {
reverseForVal(head);
int[] result = new int[list.size()];
for(int i=0;i
“`java
public int[] reverseBookList(ListNode head) {
List list = new ArrayList<>();
handler(head,list);
//return list.toArray();
int[] array = new int[list.size()];
for (int i=0; ivoid handler(ListNode node, List list) {
if (null node) return;
handler(node.next,list);
list.add(node.val);
}
共以下 65 个回答
初始化temp和count
利用temp进行遍历查看最开始的head有多少个元素,用count计数
获取到count的值也就是head的个数后,创建res数组,大小为count
while遍历head
k=count-1 也就是最后一个位置
把head每一个值,放入res中,位置是k 每一次遍历从后往前 就是k–
每一次遍历head都要往后移动,直到尾部null退出循环
”’python
def reverseBookList(self, head):
“””
:type head: Optional[ListNode]
:rtype: List[int]
“””
#用递归,分解为子问题:头节点+反转子列表
if head is None:
return []
return self.reverseBookList(head.next) + [head.val]
时间复杂度O(n),空间复杂度O(n)
解释:时间复杂度:假设链表长度为 n,对于每个节点都会进行递归操作,因此时间复杂度可以表示为 O(n),即与链表长度成正比。
空间复杂度:在递归过程中,需要维护递归调用栈,其空间复杂度取决于递归的深度。在最坏情况下,递归深度等于链表长度 n,因此空间复杂度为 O(n)。此外,还需要额外的空间来存储每个节点值构成的列表,列表的长度也为 n,因此额外空间复杂度也为 O(n)。
//反转链表
class Solution {
public int[] reverseBookList(ListNode head) {
if (head null) {
return new int[0];
}
int num = 0;
ListNode temp = head;
while (temp != null) {
temp = temp.next;
num++;
}
int[] returnInt = new int[num];
int index = num-1;
while (head != null) {
returnInt[index] = head.val;
index–;
head = head.next;
}
return returnInt;
}
}
/**
* 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) {}
* };
*/
#include
class Solution { reverseBookList(ListNode* head) result; resul(count);
public:
vector
{
vector
if(head nullptr) return result;
int count = 0;
ListNode* cur = head;
while(cur != nullptr)
{
count++;
cur = cur->next;
}
cur = head;
vector
int k = count-1;
while(cur != nullptr)
{
resul[k–] = cur->val;
cur = cur->next;
}
return resul;
};
//有很多种解法,目前只了解了一种,用数组的方式进行解决,后面再研究别的解法
第四次打卡
时间复杂度:O(n) 空间复杂度:O(1)
”’python
class Solution(object):
def reversePrint(self, head):
“””
:type head: ListNode
:rtype: List[int]
“””
if not head:
return []
时间复杂度O(N),空间复杂度O(1)v; reverseBookList(ListNode* head) {
class Solution {
public:
vector
vector
if(headnullptr)
return v;
if(head->nextnullptr){
v.push_back(head->val);
return v;
}
reverseBookList(head->next);
v.push_back(head->val);
return v;
}
};
时间复杂度:O(n)
空间复杂度:O(1)
class Solution {
public int[] reverseBookList(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 index = count – 1;
while (head != null) {
res[index–] = head.val;
head = head.next;
}
return res;
}
}
时间复杂度O(n)
先计算出链表长度,再创建该长度的数组,再从尾到头插入链表数据
“`C++ reverseBookList(ListNode* head) { res(len);
//输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
class Solution {
public:
vector
if(head == nullptr){
return {};
}
int len = 0;
ListNode *temp = head;
while(temp != nullptr){
len++;
temp = temp->next;
}
vector
int k=len-1;
while(head != nullptr){
res[k] = head->val;
head = head->next;
k–;
}
return res;
}
};
“`
class Solution {
int[] res;
public int[] reverseBookList(ListNode head) {
int count=0;
ListNode pre=null;
while(head!=null)
{
count++;
ListNode temp=head.next;
head.next=pre;
pre=head;
head=temp;
}
res=new int[count];
int i=0;
while(pre!=null)
{
res[i]=pre.val;
i++;
pre=pre.next;
}
return res;
}
}
第一次反转链表记录长度,第二次直接遍历给数组赋值
方法一:递归,时间复杂度O(n),空间复杂度O(n) list = new ArrayList<>(); list = new ArrayList<>(); stack = new Stack<>();
class Solution {
List
public int[] reverseBookList(ListNode head) {
recursion(head);
int[] arr = new int[list.size()];
for(int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public void recursion(ListNode cur) {
if(cur null) {
return;
}
reverseBookList(cur.next);
list.add(cur.val);
}
}
方法二:栈,时间复杂度O(n),空间复杂度O(n)
class Solution {
List
public int[] reverseBookList(ListNode head) {
Stack
while(head != null) {
stack.add(head.val);
head = head.next;
}
int[] arr = new int[stack.size()];
int i = 0;
while(!stack.isEmpty()) {
arr[i] = stack.pop();
i++;
}
return arr;
}
}
思路:第一遍遍历计算链表长度,第二遍遍历计算数值
“`c++ 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
if(head == NULL) return {}; //返回为空的数组
ListNode* tmp;
tmp = head;
public int[] reverseBookList(ListNode head) { list =new ArrayList<>();
List
while(head!=null){
list.add(head.val);
head=head.next;
}
int n=list.size();
int[] ans =new int[n];
for(int i=0;i<n;i++) ans[i]=list.get(n-1-i);
return ans;
}
思路:把数据存在数组中 倒序访问数组 双O(n)
没注意审题 以为要用尾插法重新构造链表
/** ret; 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
if(head nullptr) return {};
if(head->next != nullptr)
reverseBookList(head->next);
ret.push_back(head->val);
return ret;
}
};
这里用的递归,也可用栈实现。
时间空间复杂度都是O(n)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode next;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int reverseBookList(struct ListNode* head, int* returnSize) {
struct ListNodep=head;
int cnt=0;
while(p){ //遍历链表得到链表结点数量
cnt++;
p=p->next;
}
*returnSize=cnt;
int ans=(int)malloc(cntsizeof(int));
int i=cnt-1;
p=head;
while(p){
ans[i–]=p->val; //倒序将链表元素放入数组
p=p->next;
}
return ans;
}打卡
#打卡
#栈
var reverseBookList = function(head) {
let arr = [];
let node = head;
while(node){
arr.unshift(node.val);
node = node.next;
}
return arr;
};
#递归
var reverseBookList = function(head) {
if(!head) return []
let result = reverseBookList(head.next)
result.push(head.val);
return result;
};
“`C++ reverseBookList(ListNode* head) { ();
class Solution {
public:
vector
if (!head) return vector
};
“`
时间复杂度 O(n)
空间复杂度 O(1)
时间:O(n)
空间:O(n)
思路:
从头开始遍历链表,存到数组容器当中,(类似栈的原理,先进后出)。输出数组,即逆序输出单链表。
遍历链表,存数组时候使用了递归,
class Solution { reverseBookList(ListNode* head) { res;
public:
vector
recur(head);
return res;
}
private:
vector
void recur(ListNode *head){
if(head nullptr) return;
recur(head->next);
res.push_back(head->val);
}
};
/**
* 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) {
if(head null) return new int[0];
}
时间复杂度O(n),空间复杂度 O(n)
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
其余时候将当前的val的+到最后
时间复杂度:o(N)
空间复杂度:o(N)
java tmp = new ArrayList ();
class Solution{
ArrayList
public int[] reverseBookList(ListNode head){
recur(head);
int[] res = new int[tmp.size()];
for(int i = 0; i < res.length; i++){
res[i] = tmp.get(i);
}
return res;
}
void recur(ListNode head){
if(head null) return;
recur(head.next);
tmp.add(head.val);
}
}
提交
先反转再输出
/**
* 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 count = 0;
ListNode temp = head;
while(temp != null){
count++;
temp = temp.next;
}
int[] ans = new int[count];
}
时空复杂度o(n)
时间复杂度o(n)空间复杂度o(n) reverseBookList(ListNode head) { a(count);
将链表进行遍历,反向放入数组即可 需提前获取链表长度以开辟等大的数组
/**
* 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
if(headnullptr)
{
return {};
}
ListNode *temp = head;
int count = 0;
while(temp!=nullptr)
{
count ++;
temp = temp->next;
}
vector
int k = count-1;
while(head!=nullptr)
{
a[k–] = head->val;
head = head->next;
}
return a;
};
直接数组
/**
* 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) {
ListNode node = head;
int len = 0;
while(node != null){
node = node.next;
len++;
}
int[] ans = new int[len–];
while(head != null){
ans[len–] = head.val;
head = head.next;
}
return ans;
}
使用栈存储链表,输出到数组中
时间复杂度为O(N),空间复杂度为O(N),
时间复杂度O(n),空间复杂度O(1)
时间复杂度O(n),空间复杂度O(n)
class Solution { reverseBookList(ListNode* head) {(); result(count);
public:
vector
if(headnullptr) return vector
int count=0;
ListNode *p=head;
while(p!=nullptr)
{
count++;
p=p->next;
}
vector
p=head;
for(int i=count-1; i >= 0; i–)
{
result[i] = p->val;
p = p->next;
}
return result;
}
};
打卡
时间复杂度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) {
//递归解
if(head null){
return new int[0];
}
int len = 0;
ListNode temp = head;
while(temp!=null){
len++;
temp = temp.next;
}
int [] res = new int[len];
for(int i =len-1;i>=0;i–){
res[i] = head.val;
head = head.next;
}
int* reverseBookList(struct ListNode* head, int* returnSize){
struct ListNode *temp = head;
int count = 0;
while(temp){
count++;
temp = temp->next;
}
*returnSize = count;
int *array = malloc(count * sizeof(int));
int k = count -1;
temp = head;
while(temp)
{
array[k–] = temp->val;
temp = temp->next;
}
return array;
}
思路:从右向左遍历
时间复杂度:O(n);空间复杂度O(N)
// 从右边=往左填充
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;
}
时间复杂度o(n)
空间复杂度o(n)
递归暂时不太理解 reverseBookList(ListNode* head) { st; res;
···
class Solution {
public:
vector
stack
vector
int tmp=0;
if(headnullptr)return res;
ListNode*cur=head;
};
“`
空间复杂度::O(N)
时间复杂度:O(N)
递归方式实现链表倒序打印
时间复杂度O(n)
空间复杂度O(n)
辅助栈法
时间复杂度$O(n)$
空间复杂度$O(n)$
递归法
时间复杂度同上,空间复杂度同上,我个人更喜欢递归
/** arr = new ArrayList<>();
* 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) {
ListNode pre = null, cur = head;
while (cur != null) {
ListNode tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
List
while (pre != null) {
arr.add(pre.val);
pre = pre.next;
}
int[] res = new int[arr.size()];
for (int i = 0; i < arr.size(); i++) {
res[i] = arr.get(i);
}
return res;
}
}
时间复杂度O(n)
/** tmp = new ArrayList ();
* 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 {
ArrayList
public int[] reverseBookList(ListNode head) {
recur(head);
int[] res = new int[tmp.size()];
for(int i = 0;i < res.lengtn; i++){
res[i] = tmp.get(i);
}
return res;
}
使用递归思想,一步一步深入,将值存在一个新的数组里面,后面在进行输出。
时间复杂度O(n)
空间复杂度O(n)
不足之处:思想能接受,但代码写不出
Definition for singly-linked list.
class ListNode:
def init(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head None:
return []
res = []
cur = head
def reverseList(cur:Optional[ListNode]):
if cur.next None:
res.append(cur.val)
else:
reverseList(cur.next)
res.append(cur.val)
reverseList(cur)
return res
时间o(N)需完整遍历一遍 空间o(N)
时间复杂度O(n)
空间复杂度O(n)
新建数组,倒序保存链表的值
/** list = new ArrayList ();
* 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 {
private ArrayList
private void recu(ListNode head){
if(head.next != null)
recu(head.next);
list.add(head.val);
}
}
时间复杂度On
空间复杂度On
时间复杂度o(n)
空间复杂度o(n)
#CPP
class Solution { temp = new ArrayList ();
ArrayList
public int[] reverseBookList(ListNode head) {
recur(head);
int[] res = new int[temp.size()];
for(int i = 0; i < res.length; i++){
res[i] = temp.get(i);
}
return res;
}
public void recur(ListNode head){
if(head null)return;//递归出口
recur(head.next);
temp.add(head.val);
}
}
class Solution {
int[] res;
int a = 0;
int b = 0;
public int[] reverseBookList(ListNode head) {
recur(head);
return res;
}
}时间复杂度 O(N)空间复杂度O(N)
day2 第二道题
···TypeScript
function trainningPlan(head: ListNode | null): ListNode | null {
return reverse(null,head)
};
···
从尾到头打印链表java
“`python
打卡
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if not head:
return []
# 递归
resList = []
cur = self.reverse(head, resList)
return resList
/** reverseBookList(ListNode head) { ans(cnt, 0);
* 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
if(!head) return {};
ListNode* tmp = head;
int cnt = 0;
while(tmp){
tmp = tmp->next;
cnt++;
}
vector
for(int i = cnt – 1; i >= 0; i –){
ans[i] = head->val;
head = head->next;
}
return ans;
}
};
时间复杂度O(n),空间复杂度O(1)
“`java list = new ArrayList<>();
class Solution {
List
public int[] reverseBookList(ListNode head) {
reverseForVal(head);
int[] result = new int[list.size()];
for(int i=0;i
思路:递归处理,每次先去处理next节点。复杂度O(n)
代码:
“`java list = new ArrayList<>();
public int[] reverseBookList(ListNode head) {
List
handler(head,list);
//return list.toArray();
int[] array = new int[list.size()];
for (int i=0; i
void handler(ListNode node, List list) { if (null node) return; handler(node.next,list); list.add(node.val); }使用递归时,提示内存溢出
时间复杂度:O(n)
空间复杂度:O(n)
用栈做时间复杂度O(n) 空间复杂度O(n)
递归时间复杂度O(n) 空间复杂度O(1)
“`java
class Solution {
public int[] reverseBookList(ListNode head) {
ListNode head1 = new ListNode();
int i = 0;
while(head != null){
ListNode next = head.next;
head.next = head1.next;
head1.next = head;
head = next;
i++;
}
int[] result = new int[i];
ListNode cur = head1.next;
i = 0;
while(cur != null){
result[i] = cur.val;
i++;
cur = cur.next;
}
return result;
}
}
“` 反转链表常用头插法
先遍布链表,获取长度;创建数组,从数组尾部开始赋值
时间复杂度:O(n)
空间复杂度:O(n)
思路:创建一个虚拟头节点prehead,然后依次将链表反转,当head=nullptr时即反转完链表,然后根据prehead依次获取链表的值,保存到结果集
vector<int> res中。“`C++ reverseBookList(ListNode* head) { res;
class Solution {
public:
vector
vector
if (head == nullptr) return res;
ListNode *prehead = nullptr;
ListNode *cur = nullptr;
while (head != nullptr) {
cur = head->next;
head->next = prehead;
prehead = head;
head = cur;
}
while (prehead) {
res.push_back(prehead->val);
prehead = prehead->next;
}
return res;
}
};
“`