Advertisement
TheBat

lolz

Mar 30th, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. /**
  2. * Merge two linked list together to the current linked list, that is, the
  3. * header is "head". merge their nodes together to make one list, taking two
  4. * nodes alternately between the two lists, starting with the current list.
  5. * If either list runs out, all the nodes should be taken from the other
  6. * list. The first node of the new list should be taken from the current
  7. * list, assuming the list is not empty. The contents of list two are not
  8. * checked after this method call, so it does not matter what happens to
  9. * that linked list.
  10. *
  11. * e.g. if the current list contains [5, 2, 3] and the argument two contains
  12. * [7, 13, 1] shuffleMerge should cause the current list to contain [5, 2,
  13. * 7, 13, 3, 1].
  14. *
  15. * @param two
  16. * another linked list
  17. */
  18. public void shuffleMerge(TheLinkedList<T> two) {
  19. TheLinkedList<T> newList = new TheLinkedList<T>();
  20. Node<T> tempA = head;
  21. Node<T> tempB = two.head;
  22. int counter = 0;
  23. if(this.size() == 1){
  24. newList.add(tempA.getValue());
  25. while(tempB != null){
  26. newList.add(tempB.getValue());
  27. tempB = tempB.getNext();
  28. }
  29. }else if(two.size() == 1){
  30. newList.add(tempA.getValue());
  31. newList.add(tempA.getNext().getValue());
  32. tempA = tempA.getNext().getNext();
  33. newList.add(tempB.getValue());
  34. while(tempA != null){
  35. newList.add(tempA.getValue());
  36. tempA = tempA.getNext();
  37. }
  38. }else if(this.size() > two.size()){
  39. while(counter < two.size() && tempB.getNext() != null){
  40. if(counter % 2 == 0){
  41. newList.add(tempA.getValue());
  42. newList.add(tempA.getNext().getValue());
  43. tempA = tempA.getNext().getNext();
  44. counter ++;
  45. }else{
  46. newList.add(tempB.getValue());
  47. if(tempB.getNext() == null){
  48. tempB = tempB.getNext();
  49. break;
  50. }
  51. newList.add(tempB.getNext().getValue());
  52. tempB = tempB.getNext().getNext();
  53. counter ++;
  54. }
  55. }
  56. for(int i = 0; tempA != null && i<2; i++){
  57. newList.add(tempA.getValue());
  58. tempA = tempA.getNext();
  59. }
  60. if(tempB != null){
  61. newList.add(tempB.getValue());
  62. }
  63. while(tempA != null){
  64. newList.add(tempA.getValue());
  65. tempA = tempA.getNext();
  66. }
  67. }else if(this.size() <= two.size()){
  68. while(counter < this.size() && tempA != null && tempA.getNext() != null){
  69. if(counter % 2 == 0){
  70. newList.add(tempA.getValue());
  71. if(tempA.getNext() == null){
  72. tempA = tempA.getNext();
  73. break;
  74. }
  75. newList.add(tempA.getNext().getValue());
  76. tempA = tempA.getNext().getNext();
  77. counter ++;
  78. }else{
  79. newList.add(tempB.getValue());
  80. newList.add(tempB.getNext().getValue());
  81. tempB = tempB.getNext().getNext();
  82. counter ++;
  83. }
  84. }
  85. for(int i = 0;tempB != null && i<2; i++){
  86. newList.add(tempB.getValue());
  87. tempB = tempB.getNext();
  88. }
  89. if(tempA != null){
  90. newList.add(tempA.getValue());
  91. }
  92. while(tempB != null){
  93. newList.add(tempB.getValue());
  94. tempB = tempB.getNext();
  95. }
  96. }
  97.  
  98. //Transfer the data from the merged list into the current one.
  99. this.clearList();
  100. newList.reverseList();
  101. head = newList.getHead();
  102. tail = newList.getTail();
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement