Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Swap Nodes in Pairs problem solution

YASH PAL, 31 July 202418 January 2026

In these Leetcode Swap Nodes in Pairs problem solution, we have given a linked list, swap every adjacent node, and return its head. You must solve the problem without modifying the values in the list’s nodes.

Leetcode Swap Nodes in Pairs problem solution

Leetcode Swap Nodes in Pairs problem solution in Python.

def swapPairs(self, head: ListNode) -> ListNode:
    h = head
    while head and head.next:
        head.val, head.next.val = head.next.val, head.val
        head = head.next.next
    return h

Swap Nodes in Pairs problem solution in Java.

public ListNode swapPairs(ListNode head) {
        ListNode n = helper(head);
        return n; 
    }
    
    public ListNode helper(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }    
        
        ListNode tmp = head;
        ListNode next = head.next;
        
        head.next = next.next;
        next.next = tmp; 
        head.next = helper(head.next); 
        return next;
    }

Problem solution in C++.

void _swapPairs(ListNode* odd, ListNode* even, ListNode** prev) {
        if (!odd || !even) return;
        *prev = even;
        ListNode* temp = even->next;
        even->next = odd;
        odd->next = temp;
        _swapPairs(odd->next, odd->next ? odd->next->next: NULL, &odd->next);
    }
    ListNode* swapPairs(ListNode* head) {
        _swapPairs(head, head ? head->next : NULL, &head);
        return head;
    }

Problem solution in C.

struct ListNode* swapPairs(struct ListNode* head) {
    typedef struct ListNode Node;
    Node *root  = head;
    Node *prev = NULL;
    int temp;
    int count = 0;
    while(head != NULL){
        if(count %2 == 1){
            temp = prev->val;
            prev->val = head->val;
            head->val = temp;
        }
        prev = head;
        head = head->next;
        count++;
    }
    return root;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes