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
Question Link
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:
a
and b
:
p
p
point to b
b
point to a
a
point to 3
p
become a
and let a
become the next node of a
, repeat the above procedures.Question Link
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:
fast
node move forward n steps.fast
and slow
move on them together until the fast
node arrives at the end of the linked list.slow
pointer’s position is the previous position of the element to be deleted.slow
node.Question Link
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:
curA
with curB
. If they are identical, return the point of intersection. Otherwise, move on them forward.Question Link
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:
Double Pointer Method
slow
pointer that moves forward one step one time and a fast
pointer that moves forward two steps one time.x
, the number of the node from the ring entry node to the encountered node is y
, the number of the node from the encountered node to the ring entry node is z
index1
starts from the head node
, and a pointer index2
starts from the encountered node
. Each of these pointers moves forward one
step at one time. When they encounter that is the ring entrance node.