Accepted Ruby Solution


  • 0
    S
    def swap_pairs(head)
      dummy = ListNode.new(nil)
      dummy.next = head
      prev = dummy
      while !head.nil? && !head.next.nil?
        nxt = head.next
        head.next = nxt.next
        nxt.next = head
        prev.next = nxt
        prev = head
        head = head.next
      end
      return dummy.next
    end
    
    

Log in to reply
 

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