class Solution {
public ListNode swapPairs(ListNode head) {
ListNode a=head,b=head;
while(a!=null&&a.next!=null){
b=a.next;
//两两交换
int temp;
temp=a.val;
a.val=b.val;
b.val=temp;
//向后移位
a=b.next;
}
return head;
}
}
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
a=head
b=head
while(a and a.next):
b=a.next
a.val,b.val=b.val,a.val
a=b.next
return head
标签:head,ListNode,21,val,next,链表,temp,return,节点
From: https://blog.csdn.net/Runner__Binger/article/details/142183600