-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapNodesInPairs.java
47 lines (46 loc) · 1.63 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
43
44
45
46
47
/*
* Problem: Swap Nodes in Pairs
* Given a linked list, swap every two adjacent nodes and return its head.
* You may not modify the values in the list's nodes, only nodes itself may be changed.
*
* PSA: The comments were kinda annoying to write would HIGHLY recommend
* drawing this out because that is the only way to really see what is
* going on.
*/
class SwapNodesinPairs
{
public static ListNode swapPairs(ListNode head)
{
//if we call swapPairs on null or there is only one node no swap
if(head == null || head.next == null)
return head;
else
{
//blaaah this was annoying pointer things.
//so we can't lose the value of head.next
ListNode temp = head.next;
// we give the pointer head.next to head.next.next(temp.next)
// to start the swap
head.next = temp.next;
// temp.next is still pointing to *the original* head.next.next
// we need it to point to head.
temp.next = head;
//now that we have taken care of pointer things we can finally set
// head to temp (or the original head.next)
head = temp;
//all that annoying pointer stuff is needed for this
// head.next.next was maintained just for us to make this function call
head.next.next = swapPairs(head.next.next);
// once this returns all subsequent nodes have been swapped and we done
// FINALLY.
return head;
}
}
}
//just a node...
public class ListNode
{
int val;
ListNode next;
ListNode(int x) { val = x; }
}