Professional Documents
Culture Documents
Recursive:
class Solution {
public:
ListNode* reverse(ListNode* head, ListNode *before){
if(head -> next == NULL){
head -> next = before;
return head;
}
}
};
Iterative:
class Solution {
public:
while(head != NULL){
ListNode *after = head -> next;
head -> next = before;
before = head;
head = after;
}
return before;
}
};
Detect Cycle in a linked list
class Solution {
public:
bool hasCycle(ListNode *head) {
// // O(N) space
// map<ListNode*, bool> m;
// ListNode *curr = head;
// while(curr){
// if(m[curr]) return true;
// m[curr] = 1;
// curr = curr -> next;
// }
// return false;
// O(1) space
class Solution {
public:
// if either is null
if(!list1) return list2;
if(!list2) return list1;
class cmp{
public:
bool operator () (ListNode *a, ListNode *b){
return a->val > b->val ;
}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int n = lists.size();
// get smallest element of all top of list
priority_queue<ListNode*, vector<ListNode*>, cmp> pq;
for(int i = 0; i < n; i++){
if(lists[i]) pq.push(lists[i]);
}
ListNode *dummy = new ListNode(-1);
ListNode *tail = dummy;
while(!pq.empty()){
ListNode* currLow = pq.top(); pq.pop();
tail -> next = currLow;
tail = currLow;
if(currLow-> next) pq.push(currLow -> next);
}
return dummy -> next;
}
};
class Solution {
public:
while(curr){
if(after and curr -> val == after -> val){
ListNode *last = findLastDup(curr);
if(prev) prev-> next = last -> next;
else{
head = last -> next;
}
curr = last -> next;
if(curr) after = curr -> next;
}else {
prev = curr;
curr = curr -> next;
if(curr)
after = curr -> next;
}
}
return head;
}
};
class Solution
{
public:
//Function to remove duplicates from unsorted linked list.
Node * removeDuplicates( Node *head)
{
// your code goes here
Node *prev = new Node(0), *curr = head;
prev -> next = head;
map<int, bool> m;
while(curr){
if(m[curr -> data]){
// delete node
prev -> next = curr -> next;
curr = curr -> next; continue;
}
m[curr -> data] = true;
prev = curr;
curr = curr -> next;
}
return head;
}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *currA = headA, *currB = headB;
while(currA != currB){
if(!currA) currA = headB;
else currA = currA->next;
return currA;
}
};
Check if a linked list is palindrom of not with constant space
class Solution {
public:
// tc: O(N), sc: O(1);
ListNode* reverse(ListNode *head){
if(!head) return NULL;
slow = slow->next;
fast = fast->next->next;
}
// reverse the linked list to its original form as we dont need altered structure
slow -> next = reverse(slow->next);
return true;
}
};
Reorder Lists
class Solution {
public:
while(curr){
ListNode* after = curr -> next;
curr -> next = prev;
prev = curr;
curr = after;
}
return prev;
}
void reorderList(ListNode* head) {
// find first middle element
while(curr){
curr->prev = after;
curr-> next = prev;
prev = curr;
curr = after;
if(after) after = after -> next;
}
return prev;
}
K =3
return newHead;
}