您的位置:首页 > 其它

451 - 两两交换链表中的节点

2017-04-07 10:17 405 查看
4.7

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @return a ListNode
*/
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode node1 = head;
ListNode node2 = head.next;
while(node1 != null && node2 != null){
int temp = node1.val;
node1.val = node2.val;
node2.val = temp;
node1 = node2.next;
if(node1 != null){
node2 = node1.next;
}
else{
return head;
}

}
return head;// Write your code here
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: