Nine line code to solve this problem


  • -7
    L

    Just swap with the next.

    class Solution:
    def swapPairs(self, head):
        p=head;
        while(p!=None):
            if(p.next!=None):
                p.val,p.next.val=p.next.val,p.val;
                p=p.next;
            p=p.next;
        return head;

  • 0
    E

    "You may not modify the values in the list, only nodes itself can be changed."


Log in to reply
 

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