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

      1. /**
      2. * Definition for singly-linked list.
      3. * struct ListNode {
      4. * int val;
      5. * ListNode *next;
      6. * ListNode(int x) : val(x), next(NULL) {}
      7. * };
      8. */
      9. class Solution {
      10. public:
      11. void deleteNode(ListNode* node) {
      12. ListNode* del = node->next;
      13. node->val = del->val;
      14. node->next = del->next;
      15. delete del;
      16. }
      17. };

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:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13. ListNode* removeNthFromEnd(ListNode* head, int n) {
  14. ListNode* dummy = new ListNode();
  15. dummy->next = head;
  16. ListNode* runner = dummy;
  17. ListNode* chaser = dummy;
  18. for(int i = 0; i < n; i++){
  19. runner = runner->next;
  20. }
  21. while(runner->next){
  22. chaser = chaser->next;
  23. runner = runner->next;
  24. }
  25. ListNode* del = chaser->next;
  26. chaser->next = chaser->next->next;
  27. delete del;
  28. return dummy->next;
  29. }
  30. };

3. Reverse Linked List

https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/560/

reassign next pointers:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13. ListNode* reverseList(ListNode* head) {
  14. ListNode* prev = NULL;
  15. ListNode* curr = head;
  16. while(head){
  17. head = head->next;
  18. curr->next = prev;
  19. prev = curr;
  20. curr = head;
  21. }
  22. return prev;
  23. }
  24. };

4. Merge Two Sorted Lists

https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/771/

create a new list:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13. ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
  14. ListNode* dummy = new ListNode(-1);
  15. ListNode* curr = dummy;
  16. while(l1 && l2){
  17. if(l1->val <= l2->val){
  18. curr->next = new ListNode(l1->val);
  19. l1 = l1->next;
  20. curr = curr->next;
  21. } else {
  22. curr->next = new ListNode(l2->val);
  23. l2 = l2->next;
  24. curr = curr->next;
  25. }
  26. }
  27. if(l1){
  28. curr->next = l1;
  29. }
  30. if(l2){
  31. curr->next = l2;
  32. }
  33. return dummy->next;
  34. }
  35. };

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;
    }
};