My simple JAVA solution, constant space, easy understanding

  • 0

    I think it will be helpful if you draw a little listnode graph for this problem.

    public class Solution {
        public ListNode swapPairs(ListNode head) {
            ListNode current = new ListNode(Integer.MAX_VALUE);
   = head;
            ListNode dummy = current;
            while( !=null && != null){
                ListNode temp =;
       = temp;
                dummy =;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.