public class Solution {
public ListNode SwapPairs(ListNode head) {
if (head == null)
return null;
int x = 1;
int temp = 0;
ListNode temp_2 = new ListNode();
ListNode result = temp_2;
while (null != head)
{
if (x % 2 != 0)
{
temp = head.val;
head = head.next;
if(head != null)
{
temp_2.val = head.val;
temp_2.next = new ListNode();
temp_2 = temp_2.next;
temp_2.val = temp;
}
else
{
temp_2.val = temp;
}
}
else
{
head = head.next;
if(null != head)
{
temp_2.next = new ListNode();
temp_2 = temp_2.next;
}
}
x++;
}
return result;
}
}
|