Swap Nodes in Pairs
Solution in go
func swapPairs(head *ListNode) *ListNode {
    dummy := &ListNode{Next: head}
    prev := dummy
    curr := head

    for curr != nil && curr.Next != nil {
        next := curr.Next
        curr.Next = curr.Next.Next
        prev.Next = next
        next.Next = curr
        prev = curr
        curr = curr.Next
    }

    return dummy.Next
}
    TheDeveloperCafe © 2022-2024