Guest User

Untitled

a guest
Oct 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. Node preX = x.prev;
  2. Node nexX = x.next;
  3. Node preY= y.prev;
  4. Node nexY= y.next;
  5.  
  6. //edge case #1: x or y has nothing.
  7. if(x==null || y == null) return;
  8.  
  9. //edge case #2: x == y, nothing to swap
  10. if(x==y) return;
  11.  
  12. //edge case #3: edge case #3: x is right next to y
  13.  
  14. if (nexX == y)
  15. {
  16.     if(preX!=null) //if x has a previous node
  17.         preX.next = y;
  18.  
  19.     y.prev = preX;
  20.    
  21.     if(nexY!=null) //if y has a next nodel
  22.         nexY.prev = x;
  23.  
  24.     x.next = nexY;
  25.     x.prev = y;
  26.     y.next = x;
  27. }
  28. else if(nexY == x)
  29. {
  30.     if(preY != null) //if y has a previous node
  31.         preY.next = x;
  32.  
  33.     x.prev = preY;;
  34.    
  35.     if(nexX != null) //if x has a next node
  36.             nexX.prev = y;
  37.    
  38.     y.next = nexX;
  39.     y.prev = x;
  40.     x.next = y;
  41. else
  42. {
  43. //swap part
  44. y.prev = preX;
  45. y.next = nexX;
  46.    
  47. if(nexX != null) //if x has a next node
  48.     nexX.prev = y;
  49.  
  50. if(preX!=null) //if x has a previous node
  51.     preX.next = y;
  52.  
  53. x.prev = preY;
  54. x.next = nexY;
  55.  
  56. if(nexY!=null) //if y has a next nodel
  57.     nexY.prev = x;
  58.  
  59. if(preY != null) //if y has a previous node
  60.     preY.next = x;
  61. }
  62. //if either x or y was the head, the actual 'head' reference wasn't changed; so update it now. 
  63. if(head == x)
  64.     head = y;
  65. else if(head == y)
  66.     head = x;
  67.  
  68. /* we need the following if tail exists
  69. if(tail == x)
  70. tail = y;
  71. else if(tail == y)
  72. tail = x;
  73. */
Add Comment
Please, Sign In to add comment