4 lines solution using bit manipulation and coalesce

  • 0

    select s1.id, coalesce(s2.student, s1.student) as student
    from seat s1 left join seat s2
    on ((s1.id+1)^1)-1 = s2.id
    order by s1.id;

    Description: bit manipulation calculates the pair to swap, coalesce function allow the last one with no pair

Log in to reply

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