-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode24.txt
More file actions
28 lines (28 loc) · 887 Bytes
/
leetcode24.txt
File metadata and controls
28 lines (28 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* 利用三个游标进行改变即可,首先调动current,再调动first,最后调动second,然后跟新current循环
* 注意!!!最后根性current的时候,first和second已经改变了
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode current = dummy;
while(current.next != null && current.next.next != null){
ListNode first = current.next;
ListNode second = current.next.next;
current.next = second;
first.next = second.next;
second.next = first;
current = current.next.next;
}
return dummy.next;
}
}