在恒定空间和线性时间内向后打印单向链表 [英] Print a singly-linked list backwards, in constant space and linear time

查看:19
本文介绍了在恒定空间和线性时间内向后打印单向链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听到一个面试问题:

"向后打印单向链表,在恒定空间和线性时间中."

"Print a singly-linked list backwards, in constant space and linear time."

我的解决方案是原地反转链表,然后像这样打印.有没有另一种非破坏性的解决方案?

My solution was to reverse the linkedlist in place and then print it like that. Is there another solution that is nondestructive?

推荐答案

如果在打印后再次反转它将不再具有破坏性,因为恢复了原来的顺序.

If you reverse it again after printing it will no longer be destructive, since the original order is restored.

这篇关于在恒定空间和线性时间内向后打印单向链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆