random - Shuffling an linked list Java -
I have a hard time using a pseudo-code for tampered algorithm and change it into a working Java code has given. I am attempting to manipulate a linked list as a whole, the method takes the indicator of the head of the linked list and gives a list pointing at the top of the same list. I want to use a getLength and getItem method that I have created.
Public Static ListElement shuffle (ListElement head) {head = head.getLength (); ListElement head2 = Faucet; While (head == empty) {int random = (int) Math.random () * n; For (int i = 0; i & lt; random; i ++) {head = head.getNext (); }} Return head; } Pseudo code:
A list of lengths L. A new list R is empty, while L is not empty A random K such that 0 & Lt; = K & lt; Remove the element element of L = (length L) and repeat it with e
I just rewrite the code a bit so that it can follow the phonetic code.
ListElement head2 = null; Int length = head.getLength (); While (head! = Zero) {int k = (int) Math.random () * Length; // Assume that the function is deleteAt (index) which removes the element on a specific index and removed the // element listE = e head. Delitat (K) returns; // Although I can just give the implementation - I will leave // as exercise. // You can add element to front / head2.addFront (e) which is a function; E.setNext (head2); Head2 = e; // Reduce the length of length // instead of the length again; -; } Return head;
Comments
Post a Comment