Shuffling an linked list Java

时光毁灭记忆、已成空白 提交于 2019-12-24 06:47:21

问题


I'm having a very hard time using a pseudo code for the shuffling algorithm and turning it to a working java code. I'm attempting to shuffle a linked list. Overall the method takes the pointer of the head of the linked list and returns a pointer to the head of the same list randomly. I want to use a getLength and getItem method I've created.

public static ListElement shuffle(ListElement head){
  head = head.getLength();
  ListElement head2= null;
  while( head == null) {
    int random = (int) Math.random() *  n;
    for(int i=0;i<random;i++){
        head= head.getNext();
    }
  }
  return head;    
}

Pseudo code:

A list L of length n
A new list R, empty
while L is not empty
   pick a random k 
   such that 0<=k<= (length L)
   remove the kth element of L
      call it e
   prepend e to R

回答1:


I just rewrite the code a bit so that it follows the pseudo code.

ListElement head2 = null;
int length = head.getLength();

while (head != null) {
    int k = (int) Math.random() * length;

    // Assume there is function deleteAt(index) that removes
    // the element at specified index and returns the deleted
    // element
    ListElement e = head.deleteAt(k);
    // Although I can just give the implementation - I'll leave this
    // as exercise.

    // You can have a function that add element to front
    // head2.addFront(e);
    e.setNext(head2);
    head2 = e;

    // Instead of querying the length again
    // decrement the length
    length--;
}
return head;



回答2:


head = head.getLength();

Looks as if it should be int n = head.getLength();

while( head == null) {

Looks as if it should be while (head != null) {

int random = (int) Math.random() *  n;
for(int i=0;i<random;i++){
    head= head.getNext();

You're overwriting the head variable. You need to use a new variable to find the kth element of the list, and leave head pointing to the old list.

You don't do anything yet with the element once you've found it, you need to extract it from the old list (hard) and prepend it to the new list.

return head;

You need to return the new list.




回答3:


The problem with your pseudocode is that the worst case scenario, you will have to iterate to the end of the original list every time you wanted to remove an element to add it to the new list.

Original list o = [a,b,c,d,e,f,g]
New list: n = []

o = [a,b,c,d,e,f]
n = [g]

o = [a,b,c,d,e]
n = [g,f]

o = [a,b,c,d]
n = [g,f,e]

...

The best performing answer I can think of now is to create an array the size of the list and iterate through the original linked list, inserting elements to the array at random locations:

Original list o = [a,b,c,d,e,f,g]
New array a = [,,,,,,]

o = [b,c,d,e,f,g]
a = [,,a,,,,]

o = [c,d,e,f,g]
a = [,,a,,b,,]

o = [d,e,f,g]
a = [c,,a,,b,,]

o = [e,f,g]
a = [c,,a,,b,d,]

...

After you have them in the array, loop through the array and fix the links.

In the original, you'd have to call getNext() 6 times, then 5 times, then 4 times, then 3 times...

In mine, you call getNext() 6 times, then loop through the array resetting your 'next' references.



来源:https://stackoverflow.com/questions/11443233/shuffling-an-linked-list-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!