Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List
Directory
LeetCode 24. Swap Nodes in Pairs
Solution:
/**
* 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 Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0, head);
ListNode pre = dummy;
ListNode temp = null;
while(pre.next != null && pre.next.next!=null){
pre.next = head.next;
temp = head.next.next;
head.next.next = head;
head.next = temp;
pre = head;
head = head.next;
}
return dummy.next;
}
}
Thoughts:
- According to the following image, we successfully change the positions of
a
andb
:- define a dummy head
p
- let
p
point tob
- let
b
point toa
- let
a
point to3
- define a dummy head
- Then let
p
becomea
and leta
become the next node ofa
, repeat the above procedures.
LeetCode 19. Remove Nth Node From End of List
Solution:
/**
* 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 Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode fast = dummy;
ListNode slow = dummy;
// As long as there is a difference of n nodes between the fast and slow pointers.
for(int i = 0; i < n; i++)
fast = fast.next;
while(fast.next!=null){
fast = fast.next;
slow = slow.next;
}
// At this time, the `slow` pointer's position is the previous position of the element to be deleted.
slow.next = slow.next.next;
return dummy.next;
}
}
Thoughts:
- Let the
fast
node move forward n steps. - Let
fast
andslow
move on them together until thefast
node arrives at the end of the linked list. - At this time, the
slow
pointer’s position is the previous position of the element to be deleted. - Finally, delete the next node of the
slow
node.
LeetCode 160. Intersection of Two Linked Lists
Solution:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0;
int lenB = 0;
// calculate the length of linkedlist A
while(curA != null){
curA = curA.next;
lenA++;
}
// calculate the length of linkedlist B
while(curB != null){
curB = curB.next;
lenB++;
}
curA = headA;
curB = headB;
int gap = 0;
// calculate the gap and move the pointer of the longer linked list until it aligns with the start position of the other linked list.
if(lenA > lenB){
gap = lenA - lenB;
while(gap-- > 0)
curA = curA.next;
}
else{
gap = lenB - lenA;
while(gap-- > 0)
curB = curB.next;
}
while(curA !=null){
if(curA == curB)
return curA;
curA = curA.next;
curB = curB.next;
}
return null;
}
}
Thoughts:
- Calculate the length of two linked lists
- Calculate the gap between the length of two linked lists.
- Move the pointer of the longer linked list until it aligns with the start position of the other linked list.
- Compare
curA
withcurB
. If they are identical, return the point of intersection. Otherwise, move on them forward.
LeetCode 142. Linked List Cycle II
Solution:
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){ // exist a ring
ListNode index1 = slow;
ListNode index2 = head;
while(index1 != index2){
index1 = index1.next;
index2 = index2.next;
}
if(index1 == index2)
return index1;
}
}
return null;
}
}
Thoughts:
- We adopted
Double Pointer Method
- Determine whether there is a ring.
- Define a
slow
pointer that moves forward one step one time and afast
pointer that moves forward two steps one time. - If they meet halfway, it demonstrates that there is a ring.
- Define a
- If there is a ring,how to find the entrance of the ring?
- Suppose the number of nodes from the head node to the ring entry node is
x
, the number of the node from the ring entry node to the encountered node isy
, the number of the node from the encountered node to the ring entry node isz
- A pointer
index1
starts from thehead node
, and a pointerindex2
starts from theencountered node
. Each of these pointers moves forwardone
step at one time. When they encounter that is the ring entrance node.
- Suppose the number of nodes from the head node to the ring entry node is