InterviewBit - Swap list nodes in pairs
by ne on 2021-09-29 under Algo/DS/Problems tagged with interviewbit
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode A) {
ListNode temp=A;
while(temp!=null){
int a=temp.val;
if(temp.next!=null){
ListNode prev=temp;
temp=temp.next;
int b=temp.val;
prev.val=b;
temp.val=a;
}
temp=temp.next;
}
return A;
}
}