Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes
Created June 7, 2017 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InterviewBytes/1f478196d5fabc1beb7bcfe74eda5b5a to your computer and use it in GitHub Desktop.
Save InterviewBytes/1f478196d5fabc1beb7bcfe74eda5b5a to your computer and use it in GitHub Desktop.
Swap Pairs in Linked List
package com.interviewbytes.linkedlists;
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
package com.interviewbytes.linkedlists;
public class SwapPairs {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode next = head.next.next;
ListNode temp = head;
head = head.next;
head.next = temp;
head.next.next = swapPairs(next);
return head;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment