how to reverse a list with O(1) space and O(n) time?

前端 未结 8 1557
梦毁少年i
梦毁少年i 2020-12-28 19:56

I am looking for a method that reverses the same instance of a given list, with O(1) additional space and O(n) time.
this is not HW nor I am looking for some library me

8条回答
  •  执笔经年
    2020-12-28 20:19

    Just read one of the following. It is the thing you're talking about.

    Please note that we're talking about singly 'linked' lists.

    http://www.teamten.com/lawrence/writings/reverse_a_linked_list.html

    http://www.mytechinterviews.com/reverse-a-linked-list

    http://www.geekpedia.com/code48_Reverse-a-linked-list.html

    http://www.codeproject.com/KB/recipes/ReverseLinkedList.aspx

    Plus an extra question for you:

    How would you find Nth element from the tail of a linked list assuming it is singly linked and you have only head pointer with O(1) space and O(N) time?

提交回复
热议问题