Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. The values in the list's nodes cannot be modified; only the nodes themselves may be changed.

Constraints

  • The number of nodes in the list is in the range [0, 100].
  • 0 <= Node.val <= 100
Auto-saved · 10 lines
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next

class Solution:
def swapNodesInPairs(self, head: ListNode) -> ListNode:


Console

Click Run to execute sample tests.