Introduction to Linked Lists#
Definition of Linked List#
// Singly Linked List
struct ListNode {
int val; // Element stored in the node
ListNode *next; // Pointer to the next node
ListNode(int x) : val(x), next(NULL) {} // Constructor for the node
};
Initialize the node using a custom constructor:
ListNode* head = new ListNode(5);
Deleting and Adding in Linked List#
Removing Elements from Linked List#
Set a dummy head node when performing node removal operations:
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyHead = new ListNode(0); // Set a dummy head node
dummyHead->next = head; // Point the dummy head to head, making deletion easier
ListNode* cur = dummyHead;
while (cur->next != NULL) {
if(cur->next->val == val) {
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
} else {
cur = cur->next;
}
}
head = dummyHead->next;
delete dummyHead;
return head;
}
};
Designing a Linked List#
class MyLinkedList {
public:
// Define the structure of linked list nodes
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val):val(val), next(nullptr){}
};
// Initialize the linked list
MyLinkedList() {
_dummyHead = new LinkedNode(0); // The head node defined here is a dummy head, not the actual head of the linked list
_size = 0;
}
// Get the value of the node at the index, return -1 if index is invalid, note that index starts from 0, the 0th node is the head node
int get(int index) {
if (index > (_size - 1) || index < 0) {
return -1;
}
LinkedNode* cur = _dummyHead->next;
while(index--){ // If --index, it will enter an infinite loop
cur = cur->next;
}
return cur->val;
}
// Insert a node at the front of the linked list, after insertion, the newly inserted node becomes the new head of the linked list
void addAtHead(int val) {
LinkedNode* newNode = new LinkedNode(val);
newNode->next = _dummyHead->next;
_dummyHead->next = newNode;
_size++;
}
// Add a node at the end of the linked list
void addAtTail(int val) {
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(cur->next != nullptr){
cur = cur->next;
}
cur->next = newNode;
_size++;
}
// Insert a new node before the node at index, for example, if index is 0, the newly inserted node becomes the new head of the linked list.
// If index equals the length of the linked list, it means the newly inserted node is the tail of the linked list
// If index is greater than the length of the linked list, return null
// If index is less than 0, insert the node at the head
void addAtIndex(int index, int val) {
if(index > _size) return;
if(index < 0) index = 0;
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(index--) {
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
_size++;
}
// Delete the node at index, if index is greater than or equal to the length of the linked list, return directly, note that index starts from 0
void deleteAtIndex(int index) {
if (index >= _size || index < 0) {
return;
}
LinkedNode* cur = _dummyHead;
while(index--) {
cur = cur ->next;
}
LinkedNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
// The delete command indicates that the memory originally pointed to by the tmp pointer has been released,
// the value (address) of the pointer tmp after being deleted is not NULL, but a random value. That is, after being deleted,
// if you do not add a statement tmp=nullptr, tmp will become a dangling pointer
// If the subsequent program accidentally uses tmp, it will point to unpredictable memory space
tmp=nullptr;
_size--;
}
// Print the linked list
void printLinkedList() {
LinkedNode* cur = _dummyHead;
while (cur->next != nullptr) {
cout << cur->next->val << " ";
cur = cur->next;
}
cout << endl;
}
private:
int _size;
LinkedNode* _dummyHead;
};
Reversing a Linked List#
Two-Pointer Method#
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* temp; // Save the next node of cur
ListNode* cur = head;
ListNode* pre = NULL;
while(cur) {
temp = cur->next; // Save cur's next node, because we will change cur->next next
cur->next = pre; // Reversal operation
// Update pre and cur pointers
pre = cur;
cur = temp;
}
return pre;
}
};
Recursive Method#
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur == NULL) return pre;
ListNode* temp = cur->next;
cur->next = pre;
// This can be compared with the two-pointer method code, the recursive writing actually does these two steps
// pre = cur;
// cur = temp;
return reverse(cur,temp);
}
ListNode* reverseList(ListNode* head) {
// The initialization logic is the same as the two-pointer method
// ListNode* cur = head;
// ListNode* pre = NULL;
return reverse(NULL, head);
}
};