-
Notifications
You must be signed in to change notification settings - Fork 3
/
_0024_SwapNodesInPairs.java
48 lines (42 loc) · 1.49 KB
/
_0024_SwapNodesInPairs.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.diguage.algorithm.leetcode;
import com.diguage.algorithm.util.ListNode;
import java.util.Objects;
import static com.diguage.algorithm.util.ListNodeUtils.build;
import static com.diguage.algorithm.util.ListNodeUtils.printListNode;
import static java.util.Arrays.asList;
/**
* = 24. Swap Nodes in Pairs
*
* https://leetcode.com/problems/swap-nodes-in-pairs/[Swap Nodes in Pairs - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-02-03 20:12
*/
public class _0024_SwapNodesInPairs {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Swap Nodes in Pairs.
* Memory Usage: 37.3 MB, less than 5.50% of Java online submissions for Swap Nodes in Pairs.
*/
public ListNode swapPairs(ListNode head) {
if (Objects.isNull(head) || Objects.isNull(head.next)) {
return head;
}
ListNode result = head.next;
ListNode prevNode = new ListNode(0);
prevNode.next = head;
while (Objects.nonNull(head) && Objects.nonNull(head.next)) {
ListNode second = head.next;
head.next = second.next;
second.next = head;
prevNode.next = second;
prevNode = head;
head = head.next;
}
return result;
}
public static void main(String[] args) {
_0024_SwapNodesInPairs solution = new _0024_SwapNodesInPairs();
ListNode r1 = solution.swapPairs(build(asList(1, 2, 3, 4)));
printListNode(r1);
}
}