Palindrome Linked List

今天刷leetcode的Palindrome Linked List這道題,要求判斷一個單鏈表是不是一個回文串,要求空間O(1) 時間O(n).

最簡單辦法就是反轉(zhuǎn)后面一半的鏈表,在一次比較就行了。但是苦于不知道是否能修改原始輸入值,遲遲不敢這樣做。
無奈看了下討論區(qū):
有大神@wangmenghui就提出了 Reversing a list is not considered "O(1) space" 這樣的觀點(diǎn)非常有意思。

先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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 怎樣讓自己和他人在生活中做出最合理的決策,并付出實(shí)際行動? 你是不是總碰到這樣的情況。 1.雖然很煩,手機(jī)頻...
    幸福其實(shí)很簡單111閱讀 451評論 0 2
  • 說實(shí)話,關(guān)于什么是智商,感覺一直是一個比較模糊的概念,原先的理解,智商就是人的聰明的程度,本應(yīng)該就是天生的嘛,天生...
    戒得草堂閱讀 180評論 0 0
  • 站著 挺直脊梁 昂首 那是太陽的方向
    耳冬沉閱讀 731評論 0 1
  • 我真的不知道該怎么辦,我不知道該怎么說分手,要是你沒有這么快找到男朋友,我也不至于這樣難辦了。菩薩保佑我們都能在終...
    小火人毛毛閱讀 150評論 0 0