MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/medium/_0024/Solution.java:13–39  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2018/01/31 desc :

Source from the content-addressed store, hash-verified

11 * </pre>
12 */
13public 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected