# Swap Nodes Pairs in Link List - Leet Code Solution

September 16, 2019

## Problem Statement

Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list’s nodes, only nodes itself may be changed.

``````Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.``````

## Solution

• Lets look at the first two nodes first.
• We need to move head to second. (One time operation)
• Move next of First to next of Second
• Move next of Second to First
• All looks fine. We can go like this in pairs.
• Now, what about joining the pairs?
• We need another pointer who will be responsible of joining these pairs.

## Code

``````public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
ListNode t = this;
while (t != null) {
sb.append(t.val).append(" -> ");
t = t.next;
}
return sb.toString();
}
}

ListNode prev = null;

ListNode result = second;
while (first != null && second != null) {
first.next = second.next;
second.next = first;

if (prev != null) {
prev.next = second;
}

prev = first;

first = first.next;
if (first != null) {
second = first.next;
}
}

return result;
}``````

## Leet Code submission result

Runtime: 0 ms, faster than 100.00% of Java online submissions for Swap Nodes in Pairs. Memory Usage: 34.5 MB, less than 100.00% of Java online submissions for Swap Nodes in Pairs.

## Similar Posts

### Three Sum - Leet Code Solution

Problem Statement Given an array nums of n integers, are there elements a, b, c…

### Insertion Sort Algorithm

Its a kind of incremental insertion technique, where the algorithm build up…

### Maximum Length of Subarray With Positive Product - Leet Code Solution

Problem Statement Maximum Length of Subarray With Positive Product. Given an…

### Two Sum Problem - Leet Code Solution

Problem Statement Given an array of integers, return indices of the two numbers…

### Binary Tree - Level Order Traversal

Problem Statement Given a Binary tree, print out nodes in level order traversal…

### Leetcode - Rearrange Spaces Between Words

Problem Statement You are given a string text of words that are placed among…

## Latest Posts

### Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

### How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

### How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

### Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

### Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

### Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…