Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Node preX = x.prev;
- Node nexX = x.next;
- Node preY= y.prev;
- Node nexY= y.next;
- //edge case #1: x or y has nothing.
- if(x==null || y == null) return;
- //edge case #2: x == y, nothing to swap
- if(x==y) return;
- //edge case #3: edge case #3: x is right next to y
- if (nexX == y)
- {
- if(preX!=null) //if x has a previous node
- preX.next = y;
- y.prev = preX;
- if(nexY!=null) //if y has a next nodel
- nexY.prev = x;
- x.next = nexY;
- x.prev = y;
- y.next = x;
- }
- else if(nexY == x)
- {
- if(preY != null) //if y has a previous node
- preY.next = x;
- x.prev = preY;;
- if(nexX != null) //if x has a next node
- nexX.prev = y;
- y.next = nexX;
- y.prev = x;
- x.next = y;
- else
- {
- //swap part
- y.prev = preX;
- y.next = nexX;
- if(nexX != null) //if x has a next node
- nexX.prev = y;
- if(preX!=null) //if x has a previous node
- preX.next = y;
- x.prev = preY;
- x.next = nexY;
- if(nexY!=null) //if y has a next nodel
- nexY.prev = x;
- if(preY != null) //if y has a previous node
- preY.next = x;
- }
- //if either x or y was the head, the actual 'head' reference wasn't changed; so update it now.
- if(head == x)
- head = y;
- else if(head == y)
- head = x;
- /* we need the following if tail exists
- if(tail == x)
- tail = y;
- else if(tail == y)
- tail = x;
- */
Add Comment
Please, Sign In to add comment