1. Delete Node in a Linked List
https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/553/
move the next value into the current one, and delete the next one
2 constraints related to this method:
- The node to be deleted is in the list and is not a tail node
I can modify the value in a node
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public:void deleteNode(ListNode* node) {ListNode* del = node->next;node->val = del->val;node->next = del->next;delete del;}};
2. Remove Nth Node From End of List
https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/603/
runner and chaser method:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/class Solution {public:ListNode* removeNthFromEnd(ListNode* head, int n) {ListNode* dummy = new ListNode();dummy->next = head;ListNode* runner = dummy;ListNode* chaser = dummy;for(int i = 0; i < n; i++){runner = runner->next;}while(runner->next){chaser = chaser->next;runner = runner->next;}ListNode* del = chaser->next;chaser->next = chaser->next->next;delete del;return dummy->next;}};
3. Reverse Linked List
https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/560/
reassign next pointers:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/class Solution {public:ListNode* reverseList(ListNode* head) {ListNode* prev = NULL;ListNode* curr = head;while(head){head = head->next;curr->next = prev;prev = curr;curr = head;}return prev;}};
4. Merge Two Sorted Lists
https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/771/
create a new list:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/class Solution {public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode* dummy = new ListNode(-1);ListNode* curr = dummy;while(l1 && l2){if(l1->val <= l2->val){curr->next = new ListNode(l1->val);l1 = l1->next;curr = curr->next;} else {curr->next = new ListNode(l2->val);l2 = l2->next;curr = curr->next;}}if(l1){curr->next = l1;}if(l2){curr->next = l2;}return dummy->next;}};
sort in-place:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head;
ListNode* curr;
if(l1 && !l2){
return l1;
} else if(!l1 && l2){
return l2;
} else if(!l1 && !l2){
return NULL;
}
if(l1->val <= l2->val){
head = l1;
curr = head;
l1 = l1->next;
head->next = NULL;
}
else{
head = l2;
curr = head;
l2 = l2->next;
head->next = NULL;
}
while(l1 && l2){
if(l1->val <= l2->val){
curr->next = l1;
curr = curr->next;
l1 = l1->next;
curr->next = NULL;
} else {
curr->next = l2;
curr = curr->next;
l2 = l2->next;
curr->next = NULL;
}
}
if(l1){
curr->next = l1;
}
if(l2){
curr->next = l2;
}
return head;
}
};
5. Palindrome Linked List
https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/772/
change list into string, and apply 2 pointers:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
string s = "";
while(head){
s += head->val + '0';
head = head->next;
}
int l = 0;
int r = s.length() - 1;
while(l < r){
if(s[l] != s[r])
return false;
l++;
r--;
}
return true;
}
};
6. Linked List Cycle
https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/773/
runner and chaser:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* runner = head;
ListNode* chaser = head;
while(runner){
runner = runner->next;
if(runner)
runner = runner->next;
chaser = chaser->next;
if(runner == chaser && runner != NULL)
return true;
}
return false;
}
};
