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
}
}
/**
* 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
}
}
相关文章推荐
- lintcode-451-两两交换链表中的节点
- lintcode 451 两两交换链表中的节点
- [LintCode 451] 两两交换链表中的节点(Python)
- 两两交换链表中的节点
- 两两交换链表中的节点 - C++
- 两两交换链表中的节点-LintCode
- 两两交换链表中的节点
- 两两交换链表中的节点
- python_lintcode_737. Find Elements in Matrix_451. 两两交换链表中的节点
- 两两交换链表中的节点
- 两两交换链表中的节点
- 作业4.451:两两交换链表中的节点
- 两两交换链表中的节点
- LintCode 两两交换链表中的节点
- 两两交换链表中的节点
- lintcode——两两交换链表中的节点
- 两两交换链表中的节点
- 两两交换链表中的节点
- 两两交换链表中的节点
- 两两交换链表中的节点