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
Directory
Basic Concepts of Linked List
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.
LeetCode 203. Remove Linked List Elements
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:
- Removal element operation is let the
nextpointer of the node point directly to the next node of the next node. - If we want to delete the head node, we have two methods:
- Operate the original linked list
- If you want to remove the head node, it needs to write a separate piece of logic to handle it.
- Set up a
dummy head node- Handle all nodes of the original linked list by a unified method.
- When returning, the new head node is
return dummy.next
- Operate the original linked list
LeetCode 707. Design Linked List
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:
- Don’t forget to initilize the linked list
- Because there is a dummy head, so we use
i<=indexinget(index)method
LeetCode 206. Reverse Linked 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 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
- Specific procedures:
- Define a
curnode points to the head node. Define a nullprenode - Save
cur.nextbytmp - Change
cur.nexttopre. At this time, we reversed the first node. - Looping this reverse logic, continue to move
preandcur - Finally, the
curnode points to thenull. Complete reverse and return the new head nodepre.
- Define a
本文详细介绍了链表的基本概念及在LeetCode上的经典题目实现,包括删除链表元素、设计链表、反转链表等,提供了完整的代码示例及思路解析。

被折叠的 条评论
为什么被折叠?



