author: Blankj blog : http://blankj.com time : 2018/01/31 desc :
| 11 | * </pre> |
| 12 | */ |
| 13 | public class Solution { |
| 14 | // public ListNode swapPairs(ListNode head) { |
| 15 | // if (head == null || head.next == null) return head; |
| 16 | // ListNode node = head.next; |
| 17 | // head.next = swapPairs(node.next); |
| 18 | // node.next = head; |
| 19 | // return node; |
| 20 | // } |
| 21 | |
| 22 | public ListNode swapPairs(ListNode head) { |
| 23 | ListNode preHead = new ListNode(0), cur = preHead; |
| 24 | preHead.next = head; |
| 25 | while (cur.next != null && cur.next.next != null) { |
| 26 | ListNode temp = cur.next.next; |
| 27 | cur.next.next = temp.next; |
| 28 | temp.next = cur.next; |
| 29 | cur.next = temp; |
| 30 | cur = cur.next.next; |
| 31 | } |
| 32 | return preHead.next; |
| 33 | } |
| 34 | |
| 35 | public static void main(String[] args) { |
| 36 | Solution solution = new Solution(); |
| 37 | ListNode.print(solution.swapPairs(ListNode.createTestData("[1,2,3,4]"))); |
| 38 | } |
| 39 | } |
nothing calls this directly
no outgoing calls
no test coverage detected