Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ListNode swapPairs(ListNode head) {
- if(head == null || head.next == null) return head;
- ListNode dummy = new ListNode(-1);
- dummy.next = head;
- ListNode prev = dummy;
- ListNode cur = head;
- ListNode next = cur.next;
- dummy = next;
- while(next != null){
- cur.next = next.next;
- prev.next = next;
- next.next = cur;
- prev = cur;
- cur = cur.next;
- if(cur == null || cur.next == null) break;
- next = cur.next;
- }
- return dummy;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement