Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. public ListNode swapPairs(ListNode head) {
  2. if(head == null || head.next == null) return head;
  3.  
  4. ListNode dummy = new ListNode(-1);
  5. dummy.next = head;
  6. ListNode prev = dummy;
  7. ListNode cur = head;
  8. ListNode next = cur.next;
  9. dummy = next;
  10. while(next != null){
  11. cur.next = next.next;
  12. prev.next = next;
  13. next.next = cur;
  14. prev = cur;
  15. cur = cur.next;
  16. if(cur == null || cur.next == null) break;
  17. next = cur.next;
  18. }
  19. return dummy;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement