Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Merge two linked list together to the current linked list, that is, the
- * header is "head". merge their nodes together to make one list, taking two
- * nodes alternately between the two lists, starting with the current list.
- * If either list runs out, all the nodes should be taken from the other
- * list. The first node of the new list should be taken from the current
- * list, assuming the list is not empty. The contents of list two are not
- * checked after this method call, so it does not matter what happens to
- * that linked list.
- *
- * e.g. if the current list contains [5, 2, 3] and the argument two contains
- * [7, 13, 1] shuffleMerge should cause the current list to contain [5, 2,
- * 7, 13, 3, 1].
- *
- * @param two
- * another linked list
- */
- public void shuffleMerge(TheLinkedList<T> two) {
- TheLinkedList<T> newList = new TheLinkedList<T>();
- Node<T> tempA = head;
- Node<T> tempB = two.head;
- int counter = 0;
- if(this.size() == 1){
- newList.add(tempA.getValue());
- while(tempB != null){
- newList.add(tempB.getValue());
- tempB = tempB.getNext();
- }
- }else if(two.size() == 1){
- newList.add(tempA.getValue());
- newList.add(tempA.getNext().getValue());
- tempA = tempA.getNext().getNext();
- newList.add(tempB.getValue());
- while(tempA != null){
- newList.add(tempA.getValue());
- tempA = tempA.getNext();
- }
- }else if(this.size() > two.size()){
- while(counter < two.size() && tempB.getNext() != null){
- if(counter % 2 == 0){
- newList.add(tempA.getValue());
- newList.add(tempA.getNext().getValue());
- tempA = tempA.getNext().getNext();
- counter ++;
- }else{
- newList.add(tempB.getValue());
- if(tempB.getNext() == null){
- tempB = tempB.getNext();
- break;
- }
- newList.add(tempB.getNext().getValue());
- tempB = tempB.getNext().getNext();
- counter ++;
- }
- }
- for(int i = 0; tempA != null && i<2; i++){
- newList.add(tempA.getValue());
- tempA = tempA.getNext();
- }
- if(tempB != null){
- newList.add(tempB.getValue());
- }
- while(tempA != null){
- newList.add(tempA.getValue());
- tempA = tempA.getNext();
- }
- }else if(this.size() <= two.size()){
- while(counter < this.size() && tempA != null && tempA.getNext() != null){
- if(counter % 2 == 0){
- newList.add(tempA.getValue());
- if(tempA.getNext() == null){
- tempA = tempA.getNext();
- break;
- }
- newList.add(tempA.getNext().getValue());
- tempA = tempA.getNext().getNext();
- counter ++;
- }else{
- newList.add(tempB.getValue());
- newList.add(tempB.getNext().getValue());
- tempB = tempB.getNext().getNext();
- counter ++;
- }
- }
- for(int i = 0;tempB != null && i<2; i++){
- newList.add(tempB.getValue());
- tempB = tempB.getNext();
- }
- if(tempA != null){
- newList.add(tempA.getValue());
- }
- while(tempB != null){
- newList.add(tempB.getValue());
- tempB = tempB.getNext();
- }
- }
- //Transfer the data from the merged list into the current one.
- this.clearList();
- newList.reverseList();
- head = newList.getHead();
- tail = newList.getTail();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement