Palindrome Linked List

今天刷leetcode的Palindrome Linked List这道题,要求判断一个单链表是不是一个回文串,要求空间O(1) 时间O(n).

最简单办法就是反转后面一半的链表,在一次比较就行了。但是苦于不知道是否能修改原始输入值,迟迟不敢这样做。
无奈看了下讨论区:
有大神@wangmenghui就提出了 Reversing a list is not considered "O(1) space" 这样的观点非常有意思。

先mark一记。

@wangmenghui said in Reversing a list is not considered "O(1) space":
It is a common misunderstanding that the space complexity of a program is just how much the size of additional memory space being used besides input. An important prerequisite is neglected the above definition: the input has to be read-only. By definition, changing the input and change it back is not allowed (or the input size should be counted when doing so). Another way of determining the space complexity of a program is to simply look at how much space it has written to. Reversing a singly linked list requires writing to O(n) memory space, thus the space complexities for all "reverse-the-list"-based approaches are O(n), not O(1).

Solving this problem in O(1) space is theoretically impossible due to two simple facts: (1) a program using O(1) space is computationally equivalent to a finite automata, or a regular expression checker; (2) the pumping lemma states that the set of palindrome strings does not form a regular set.

Please change the incorrect problem statement.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 怎样让自己和他人在生活中做出最合理的决策,并付出实际行动? 你是不是总碰到这样的情况。 1.虽然很烦,手机频...
    幸福其实很简单111阅读 462评论 0 2
  • 说实话,关于什么是智商,感觉一直是一个比较模糊的概念,原先的理解,智商就是人的聪明的程度,本应该就是天生的嘛,天生...
    戒得草堂阅读 186评论 0 0
  • 站着 挺直脊梁 昂首 那是太阳的方向
    耳冬沉阅读 733评论 0 1
  • 我真的不知道该怎么办,我不知道该怎么说分手,要是你没有这么快找到男朋友,我也不至于这样难办了。菩萨保佑我们都能在终...
    小火人毛毛阅读 156评论 0 0