/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null) return head;
ListNode temp = head.next;
head.next = temp.next;
temp.next = head;
head = temp;
ListNode pre = head.next;
if (pre.next == null)
return head;
ListNode cur = pre.next;
while(cur != null && cur.next != null) {
temp = cur.next;
pre.next = temp;
cur.next = temp.next;
temp.next = cur;
pre = cur;
cur = pre.next;
}
return head;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。