/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode hhead = head.next;
ListNode first = hhead.next;
hhead.next = head;
ListNode second = head;
while(first != null){
head = first;
if(head.next != null){
second.next = head.next;
first = head.next.next;
head.next.next = head;
}
else{
first = head.next;
second.next = head;
}
second = head;
}
if(first == null){
second.next = null;
}
return hhead;
}
}
Swap Nodes in Pairs
最新推荐文章于 2019-11-16 09:38:04 发布