0ms Java very Easy Solution


  • 1

    Check of current and next are not null.
    Swap values.
    Move current 2 places forward.
    public class Solution {
    public ListNode swapPairs(ListNode head) {
    ListNode current = head;
    while (current!=null && current.next != null){
    int val = current.val;
    current.val = current.next.val;
    current.next.val = val;
    current = current.next.next;
    }
    return head;
    }
    }


  • 1
    H

    You are not permitted to exchange the values in nodes. Only the nodes itself can be exchanged


Log in to reply
 

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