-
Notifications
You must be signed in to change notification settings - Fork 0
/
swapNodesInPairs.java
42 lines (40 loc) · 1.43 KB
/
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
/* Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
Example 1:
Input: head = [1,2,3,4]
Output: [2,1,4,3]
Runtime: 0 ms, faster than 100.00% of Java online submissions for Swap Nodes in Pairs.
Memory Usage: 42.7 MB, less than 5.51% of Java online submissions for Swap Nodes in Pairs.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class swapNodesInPairs {
public ListNode swapPairs(ListNode head) {
if ((head == null)||(head.next == null)) {//if empty list or single element
return head;
}
ListNode nodeA = new ListNode();
ListNode nodeB = new ListNode();
nodeA = head;
nodeB = head.next;
nodeA.next = nodeB.next;
nodeB.next = nodeA;
head = nodeB;//first case is special because of list head
while ((nodeA.next != null)&&(nodeA.next.next != null)) {//still got stuff to swap
nodeB = nodeA.next;
nodeA.next = nodeB.next;
nodeA = nodeB.next;
nodeB.next = nodeA.next;
nodeA.next = nodeB;
nodeA = nodeB;
}
return head;
}
}