Algorithm for Shuffling a Linked List in n log n time

后端 未结 7 515
北荒
北荒 2021-01-30 05:37

I\'m trying to shuffle a linked list using a divide-and-conquer algorithm that randomly shuffles a linked list in linearithmic (n log n) time and logarithmic (log n) extra space

相关标签:
7条回答
  • 2021-01-30 06:28

    You could traverse over the list, randomly generating 0 or 1 at each node.

    If it is 1, remove the node and place it as the first node of the list. If its is 0, do nothing.

    loop this until you reach the end of the list.

    0 讨论(0)
提交回复
热议问题