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
The linked list is a kind of linear structure connected by pointers. Each node is composed of two parts. One is the data field
, and the other is the pointer field
(storing the pointer to the next node). The pointer field of the last node points to null
.
The arrays are contiguously distributed in the memory space, but linked lists are not contiguously distributed in the memory space.
Like this linked list, the start node is 2, and the last node is 7. Each node distributes in the different address space of memory and is connected by pointers.
Differences between array and linked list:
The length of the array is fixed
when defining. If you want to modify its length, you need to define a new array. Because when you do the insert or delete operation, you have to move every behind element forward or backward.
The length of the linked list is unfixed
. It can do dynamic insert and delete operations. So linked list suits scenes that have few queries and highly frequent insert or delete.
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 removeElements(ListNode head, int val) {
ListNode dummy = new ListNode(-1, head);
ListNode pre = dummy;
while(head != null){
if(head.val == val)
pre.next = head.next;
else
pre = head;
head = head.next;
}
return dummy.next;
}
}
Thoughts:
next
pointer of the node point directly to the next node of the next node.dummy head node
return dummy.next
Question Link
Solution:
class ListNode {
int val;
ListNode next;
ListNode(){}
ListNode(int val){
this.val = val;
}
}
class MyLinkedList {
int size;
ListNode dummyHead;
// initialize linked list
public MyLinkedList() {
size = 0;
dummyHead = new ListNode(0);
}
public int get(int index) {
if(index < 0 || index >=size)
return -1;
ListNode pre = dummyHead;
// because there is a dummy head, so we use i<=index
for(int i=0; i<=index; i++){
pre = pre.next;
}
return pre.val;
}
public void addAtHead(int val) {
addAtIndex(0, val);
}
public void addAtTail(int val) {
addAtIndex(size, val);
}
public void addAtIndex(int index, int val) {
if(index<0)
index = 0;
if(index>size)
return;
size++;
ListNode pre = dummyHead;
for(int i=0; i<index; i++){
pre = pre.next;
}
ListNode toAdd = new ListNode(val);
toAdd.next = pre.next;
pre.next = toAdd;
}
public void deleteAtIndex(int index) {
if(index < 0 || index >= size)
return;
size--;
ListNode pre = dummyHead;
for(int i = 0; i<index; i++){
pre = pre.next;
}
pre.next = pre.next.next;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
Thoughts:
i<=index
in get(index)
methodQuestion 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 reverseList(ListNode head) {
ListNode dummy = null;
ListNode cur = head;
ListNode pre = dummy;
ListNode temp = null;
while(cur != null){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
Thoughts
cur
node points to the head node. Define a null pre
nodecur.next
by tmp
cur.next
to pre
. At this time, we reversed the first node.pre
and cur
cur
node points to the null
. Complete reverse and return the new head node pre
.