/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
List<Integer> tmp = new ArrayList<>(10010);
rev(head, tmp);
int[] ans = new int[tmp.size()];
for (int i = 0; i < tmp.size(); i++) {
ans[i] = tmp.get(i);
}
return ans;
}
private void rev(ListNode p, List<Integer> list) {
if (p == null) return;
rev(p.next, list);
list.add(p.val);
}
}
栈
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
int[] s = new int[10010];
int top = -1;
while (head != null) {
s[++top] = head.val;
head = head.next;
}
int[] ans = new int[top+1];
int idx = 0;
while (top != -1) ans[idx++] = s[top--];
return ans;
}
}
def reversePrint(head:ListNode)->list[int]:
'''
用栈(一般用list作为栈):
stack = []
while head:
stack.append(head.val)
head = head.next
return stack[::-1]
'''
'''
递归:从用往左添加列表
'''
n = []
cur = head
count = 0
while cur:
count += 1
cur = cur.next
cur = head
n = [0] * count
while cur:
count -= 1
n[count] = cur.val
cur = cur.next
return 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) {
// 栈:先进后出
// 判断head是否为空,若为空,返回空数组
if (head == null) return new int[0];
Stack<ListNode> s = new Stack<ListNode>();
// 入栈
while (head != null) {
s.push(head);
head = head.next;
}
int[] res = new int[s.size()];
// 出栈,放入数组中
for (int i = 0; i < res.length; i++) {
res[i] = s.pop().val;
}
return res;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
//方法一 从数组尾部添加
if(head == null){
return new int[0];
}
int count = 0;
ListNode temp = head;
while(temp!=null){
temp = temp.next;
count++;
}
int [] res = new int[count];
int k = count -1;
while(head!=null){
res[k--] = head.val;
head = head.next;
}
return res;
//方法二 利用栈先进后出的特性
Stack<Integer> st = new Stack<Integer>();
int count = 0;
while(head!=null){
st.add(head.val);
head = head.next;
}
int [] res = new int[st.size()];
for(int i =0; i <res.length; i++){
res[i] = st.pop();
}
return res;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
//若链表为null,返回空数组
if (head == null) {
return new int[0];
}
//使用集合填充链表值
List temp = new ArrayList();
//递归填充
recur(head, temp);
//遍历填充数组
int[] array = new int[temp.size()];
for (int i = 0; i < temp.size(); i++) {
array[i] = (int) temp.get(i);
}
return array;
}
void recur (ListNode head, List temp) {
if (head == null) {
//结束递归
return;
} else {
recur(head.next, temp);
}
//从后往前添加链表值
temp.add(head.val);
}
}
时间复杂度:O(N) 遍历链表,递归N次
空间复杂度:O(N) 使用O(N)的栈空间
解法二:栈
class Solution {
public int[] reversePrint(ListNode head) {
//若链表为null,返回空数组
if (head == null) {
return new int[0];
}
Stack<ListNode> stack = new Stack<>();
while (head != null) {
stack.push(head);
head = head.next;
}
int size = stack.size();
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = stack.pop().val;
}
return array;
}
}
public int[] reversePrint(ListNode head) {
List list = new ArrayList();
addNode(head,list);
int[] arr = new int[list.size()];
for(int i = 0 ; i < arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
public void addNode(ListNode node , List list){
if(node==null){
return;
}
addNode(node.next,list);
list.add(node.val);
}
func reversePrint(head *ListNode) []int {
if head == nil {
return nil
}
pre := head
res := []int{}
for pre != nil {
res = append(res, pre.Val)
pre = pre.Next
}
for i,j := 0,len(res)-1; i<j; {
res[i], res[j] = res[j], res[i]
i++
j--
}
return res
}
class Solution {
public int[] reversePrint(ListNode head) {
// 利用动态数组保存
List<Integer> list = new ArrayList<>();
while(head != null){
list.add(head.val);
head = head.next;
}
int[] d = new int[list.size()];
for(int i = 0;i<list.size();i++){
d[i] = list.get(list.size()-1-i);
}
return d;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
traverse(head);
return res;
}
int len = 0;
int[] res;
int cur = 0;
void traverse(ListNode head) {
if(head == null) {
res = new int[len];
return;
}
len ++;
traverse(head.next);
res[cur] = head.val;
cur ++;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
ListNode p = head;
int len = 0;
//计算出链表的长度
while(p != null){
len++;
p = p.next;
}
int[] a = new int[len];
//因为数组最大下标恰好是链表的长度-1
int k = len - 1;
p = head;
//从数组最大下标处开始为数组赋值
while(p != null){
a[k--] = p.val;
p = p.next;
}
return a;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
if(head==null) //首先判断链表第一个结点(首元结点)是否为空,注意这里力扣将head作为了链表中的首元结点,这里的head是第一个结点,所以直接判断head是否为空,而不是head.next
{
return new int[0];
}
int count = 0;
ListNode p = head;
while(p!=null) //将链表的长度计算出来,new出长度相对应的数组
{
count++;
p = p.next;
}
p = head;
int []result = new int[count];
int k = count-1;
for(;k>=0;k--) //将数组从后往前记录链表中的val值,数组中最后一个存放链表中的第一个val值
{
result[k] = p.val;
p = p.next;
}
return result;
//感悟:以前学的都是理论知识,到实际操作错误很多,还需要多加练习
}
}
class Solution {
public int[] reversePrint(ListNode head) {
if (head == null) return new int[]{};
Stack<Integer> stack = new Stack();
ListNode cur = head;
while (cur != null) {
stack.push(cur.val);
cur = cur.next;
}
int[] result = new int[stack.size()];
for (int i = 0; i < result.length; i++) {
result[i] = stack.pop();
}
return result;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
if(head == null) return new int[0];
ListNode cur = new ListNode(0);
cur = head;
int count = 1;
while(cur.next != null){
count++;
cur = cur.next;
}
int[] res = new int[count];
count--;
while(head!= null){
res[count--] = head.val;
head = head.next;
}
return res;
}
}
public class Solution {
public static void main(String[] args) {
ListNode head = new ListNode(2);
ListNode cur = head;
cur.next = new ListNode(3);
cur.next.next = new ListNode(5);
int[] ints = new Solution().reversePrint(head);
System.out.println(Arrays.toString(ints));
}
public int[] reversePrint(ListNode head) {
ListNode pre = null;
ListNode cur = head;
ListNode nex;
int num = 0;
while (cur != null) {
nex = cur.next;
cur.next = pre;
pre = cur;
num++;
cur = nex;
}
//此时pre指向反向链表的头结点
int[] nums = new int[num];
for (int i = 0; i < num; i++) {
if (pre != null) {
nums[i] = pre.val;
pre = pre.next;
}
}
return nums;
}
}
class Solution2 {
public int[] reversePrint(ListNode head) {
//可以使用栈
LinkedList<Integer> stack = new LinkedList();
int num = 0;
while (head != null) {
stack.push(head.val);
num++;
head = head.next;
}
int[] nums = new int[num];
for (int i = 0; i < num; i++) {
if (!stack.isEmpty())
nums[i] = stack.pop();
}
return nums;
}
}
class Solution3 {
//递归
LinkedList<Integer> linkedList = new LinkedList();
public int[] reversePrint(ListNode head) {
getValues(head);
int [] nums=new int[linkedList.size()];
for (int i = 0; i < linkedList.size(); i++) {
nums[i]=linkedList.get(i);
}
return nums;
}
public void getValues(ListNode listNode) {
if (listNode == null) {
return;
}
getValues(listNode.next);
linkedList.add(listNode.val);
}
}
class Solution4{
//先统计数,在倒着来计算
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;
}
}
class Solution(object):
def reversePrint(self, head):
# 判空
if not head:
return []
li = []
temp = head
while temp:
li.append(0)
temp = temp.next
for i in range(len(li)-1,-1,-1):
li[i] = head.val
head = head.next
return li
class Solution {
LinkedList list = new LinkedList<>();
public int[] reversePrint(ListNode head) {
reverse(head);
int[] res = new int[list.size()];
for(int i = 0; i < list.size(); i++){
res[i] = list.get(i);
}
return res;
}
public void reverse(ListNode cur){
if(cur == null)
return;
reverse(cur.next);
list.add(cur.val);
}
}
倒序填充
class Solution {
public int[] reversePrint(ListNode head) {
ListNode cur = head;
int length = 0;
while(cur != null){
length++;
cur = cur.next;
}
cur = head;
int[] res = new int[length];
for(int i = length - 1; i >= 0; i --){
res[i] = cur.val;
cur = cur.next;
}
return res;
}
}
利用栈的结构
class Solution {
public int[] reversePrint(ListNode head) {
LinkedList<Integer> stack = new LinkedList<>();
ListNode cur = head;
int length = 0;
while(cur != null){
length++;
stack.push(cur.val);
cur = cur.next;
}
int[] res = new int[length];
for(int i = 0; i < length; i ++){
res[i] = stack.pop();
}
return res;
}
}
public int[] reversePrint(ListNode head) {
if (head == null) {
return new int[0];
}
//方法1 使用栈存储 然后出栈 时间复杂度O(n) 空间复杂度O(1)
/* Stack<Integer> stack = new Stack<>();
while (head != null) {
stack.push(head.val);
head = head.next;
}
int[] res = new int[stack.size()];
for (int i = 0; i < stack.size(); i++) {
res[i] = stack.pop();
}
return res;*/
//方法2 先逆序饿然后打印,时间复杂度O(n) 空间复杂度也为O(n) 但是省了栈到空间
ListNode pre = null;
ListNode cu = head;
int size = 0;//计算链表长度,用来初始化数组
while (cu != null) {
ListNode next = cu.next;
cu.next = pre;
pre = cu;
cu = next;
size++;
}
//反转后头节点是pre
int[] res = new int[size];
for (int i = 0; i < size; i++) {
res[i] = pre.val;
pre = pre.next;
}
return res;
}
// 用栈存储节点的值,遍历完链表后出栈
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> s = new Stack<>();
while(head != null){
s.push(head.val);
head = head.next;
}
int len = s.size();
int[] res = new int[len];
for(int i = 0; i < len; i++)
res[i] = s.pop();
return res;
}
}
// 时间复杂度: O(n)
// 空间复杂度: O(n)
public int[] reversePrint(ListNode head) {
ListNode cur = head;
int count = 0;
while(cur!=null){
cur = cur.next;
count++;
}
int arr [] = new int[count];
//重新初始辅助结点
cur = head;
while(cur!=null){
arr[--count] = cur.val;
cur = cur.next;
}
return arr;
}
class Solution {
public int[] reversePrint(ListNode head) {
//利用栈的特性解决这个问题
Deque<Integer> integerDeque = new ArrayDeque<>();
//开始遍历链表,将链表的值全部放到栈当中去
while (head!=null){
integerDeque.push(head.val);
head=head.next;
}
int[] res = new int[integerDeque.size()];
int index = 0;
while (!integerDeque.isEmpty()){
res[index]=integerDeque.pop();
index++;
}
return res;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> s = new Stack<>();
int count = 0;
while(head!=null){
s.push(head.val);
count++;
head = head.next;
}
int[] in = new int[count];
for(int i=0;i<count;i++){
in[i] = s.pop();
}
return in;
}
}
方法二:递归
class Solution {
int count;
int count1;
int[] res;
public int[] reversePrint(ListNode head) {
if(head==null){
res = new int[count];
return res;
}
count++;
reversePrint(head.next);
res[count1] = head.val;
count1++;
return res;
}
}
共以下 86 个回答
打破惯性思维 数组从尾到头存放
递归法
栈
查长度,构造数组反向遍历输出
class Solution {
public int[] reversePrint(ListNode head) {
ListNode p1 = head;
int count = 0;
while(p1!=null){
p1 = p1.next;
count++;
}
int []nums = new int[count];
p1 = head;
for(int i =count-1;i>=0;i–){
nums[i] = p1.val;
p1 = p1.next;
}
return nums;
}
“`c++“` result = {}; 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
void dfs(ListNode *head){
if(!head)
return;
dfs(head->next);
result.push_back(head->val);
}
vector
dfs(head);
return result;
}
};
递归法实现:
时间复杂度:O(n)
空间复杂度:O(n)
先查出链表长度,构建同长度数组,遍历链表给数组赋值
class Solution {
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)
1,利用递归,递归到最后一个值然后添加到链表
2,时间复杂度O(n)空间复杂度O(n)
思路: 递归实现
● 时间复杂度: O(n)
● 空间复杂度: O(n)
时间复杂度:O(N) 遍历链表,递归N次
空间复杂度:O(N) 使用O(N)的栈空间
解法二:栈
时间复杂度:O(N) 入栈和出栈使用O(N)时间
空间复杂度:O(N)
在想用遍历的方式让指针走到队尾,可是有个疑问,就算最后指向了末尾 ,要怎么取出上一个结点呢???
这个题有个特点–先进后出,利用数组,每次获取一个数就把他插入进去,这样遍历数组,读出来的顺序刚好就是相反的
递归。但是list怎么能直接取出数组?
思路:
只需要打印不需要反转,所以我们利用栈数据结构,先进后出。
时间复杂度O(n)
空间复杂度O(n)
思路:利用栈的特性,先进后出。将链表中的值先存入栈中(借助LinkedList),然后弹出栈顶元素并存入数组中。
时间复杂度: O(n)
空间复杂度: O(n)
时间复杂度:O(n)
空间复杂度:O(n)
递归 时间O(n) 空间O(n)
复杂度分析:
时间复杂度:O(n)
空间复杂度:O(1)
递归
时间复杂度O(n)
空间复杂度O(n)
1、在对与不对之间含糊不清,采用后序遍历递归法
2、时间复杂度O(n),空间复杂度O(n)
以上顺序读取,数组反转
时间复杂度O(n)
空间复杂度O(n)
先用栈实现了一下,翻转这种第一时间想到栈。主要复习一下stl的一些基本操作。
时间复杂度O(n)
空间复杂度O(n)
然后再用在leetcode题解中学到的递归实现,其使用java实现在递归到最后一个null的时候得到了所需开辟数组的长度,非常优雅(虽然vector中没用到,但是思路很棒)并且这种算法不要考虑head=nullptr,第一层递归的判断帮助完成了。
时间复杂度O(n)
空间复杂度O(n)(函数调用栈和vector)
最后用在老师视频讲解中的思路,抛开传统遍历一定从左至右的思想,也可以在添加到数据结构中的顺序为从后至前。这里体现为每个元素直接插入到vector的开头。
时间复杂度O(n)
空间复杂度O(n)
虽然三种方法的时间空间复杂度都在一个量级,但是用了栈的时间最快。
递归的实质是一个栈
解法:递归
时间复杂度: O(n)
空间复杂度: O(n)
下面是代码
第一种:递归解决,类似二叉树后序遍历的思想(运行时长高了,似乎List转int[]数组影响性能)O(n)
第二种:直接分两次遍历,先计算出链表长度,再从尾到头填入数组 O(n)
不是最优解,采用了头插法的思路,反转链表
时间复杂度:O(n)
时间复杂度 O(n)
//recursion递归遍历 res; reversePrint(ListNode* head) {
class Solution {
public:
vector
void fun(ListNode* head){
if(!head) return;
fun(head->next);
res.push_back(head->val);
return;;
}
vector
fun(head);
return res;
};
时间复杂度: O(n)
空间复杂度: O(n)
时间:O(n)
空间:O(1)
时间复杂度:O(n)
空间复杂度:O(n)
C++的STL和Java中的关于栈的操作还不会,以后还要多加练习。
栈
时间复杂度On
空间复杂度On
熟悉向vector容器中添加数,取栈顶元素以及将栈顶元素弹出栈的语法
时间 O(n)
空间 O(n)
方法一:栈
1. 时间复杂度:O(n)
2. 空间复杂度:O(n)
3. 代码
方法二:数组倒序添加元素
1. 时间复杂度:O(n)
2. 空间复杂度:O(1)
3. 代码
根据长度建数组,然后遍历往里放
用栈操作,递归也可以
递归
时间 O(N)
空间 O(N)
空间复杂度O(n)
时间复杂度O(n)
1、先准备一个vector和stack容器,如果头节点为空,则直接返回vector
2、再将链表中的节点压入栈中
3、最后将栈中的值取出放到vector中
时间复杂度O(n) 空间复杂度O(1)
//本题可以不额外使用容器,直接用数组,利用回溯算法,先得到数组总长度,再初始化数组,再回溯过程中,为数组赋值
class Solution {
int[] res;
int count;
public int[] reversePrint(ListNode head) {
process(head, 0);
return res;
}
void process(ListNode head, int i) {
if (head null) {
count = i;
res = new int[count];
return;
}
process(head.next, i + 1);
res[count – i – 1] = head.val;
}
}
解体思路:递归
时间复杂度:O(n)
空间复杂度:O(n)
// 递归解法
时间复杂度:O(n)
空间复杂度:O(n)
// 数组读出后前后依次交换
时间复杂度:O(n)
空间复杂度:O(1) // 数组本身不算
递归
简单做法
辅助栈
代码
时间复杂度:O(N)
空间复杂度:O(N)
直接利用栈的特性来可能是最简单,利用栈则可以使用递归的方法,这里暂时不给出.
复杂一点就是可以直接将链表反转,稍微复杂一些
时间复杂度和空间复杂度都为o(n)
递归解法
倒序填充
利用栈的结构
时间复杂度O(n)
空间复杂度O(n)
时间复杂度O(n)
空间复杂度O(n)
解题思路:
1.辅助结点cur,进行遍历计数,计算出链表的长度
2.给出一个数组数组的长度为链表的长度
3.初始化结点,反向遍历输出值,将其赋值给好的数组中
递归
利用栈结构就可以实现
思路一:翻转链表并从头节点开始存数据到数组中,时间复杂度O(N),空间复杂度O(N)。
思路二:先遍历一遍链表确定数组长度,然后从头节点和数组默认开始存数据。时间复杂度O(N),空间复杂度O(N)。
思路三:适合用递归实现,因为是从尾开始打印,就是说先访问到的数据后打印,栈的特性,可以借助栈来实现,递归的本质也是栈。时间复杂度O(N),空间复杂度O(N)。
给出递归实现
第二天第一题
时间复杂度:O(n),空间复杂度:O(1)
先遍历一次链表确定链表长度,再遍历一次存入数组
时间空间都是O(n)
都是O(n)
时间和空间复杂度都为O(n)
额
方法一:时间空间复杂度都为O(n)
方法二:递归
时间:O(n)
空间:O(n)