Leetcode - linked list

Hyunwoo ChoiHyunwoo Choi
2 min read

After solving 10 linked list problems, some algorithms are used over and over again. I would like to write down the ideas before I forget them again.

  1. Many questions are using a singly linked list

  2. Create and append - use two pointers

     /**
      * Definition for singly-linked list.
      * struct ListNode {
      *     int val;
      *     ListNode *next;
      *     ListNode(int x) : val(x), next(NULL) {}
      * };
      */
    
         ListNode* CreateAndAppend() {
             //need two pointers to create and append
             ListNode* head = nullptr;
             ListNode* current = head;
    
             if(head == nullptr)
             {   //create
                 head = new ListNode(1);
                 current = head;
             }
             //append
             current->next = new ListNode(2);
             current = current->next;
    
             return head;
         }
    
  3. Reverse a linked list - use two pointers

     /**
      * Definition for singly-linked list.
      * struct ListNode {
      *     int val;
      *     ListNode *next;
      *     ListNode(int x) : val(x), next(NULL) {}
      * };
      */
     class Solution {
     public:
         ListNode* reverseList(ListNode* head) {
             ListNode* next = head;
             ListNode* prev = nullptr;
    
             while(next != nullptr)
             {   //sequence is important
                 next = head->next;
                 head->next = prev;
                 prev = head;
                 head = next;
             }
             return prev;
         }
    
  4. Floyd’s cycle finding algorithm(Find cycle, mid point)

class Solution {
public:    
    bool hasCycle(ListNode *head) {
        if(head == nullptr || head->next == nullptr)
        {
            return false;
        }
        else
        {
            ListNode* slow = head;
            ListNode* fast = head;

            while(fast != nullptr && fast->next != nullptr)
            {    //one pointer moves faster than another pointer
                slow = slow->next;
                fast = fast->next->next;

                if(slow == fast)
                {
                    return true;
                }
            }
            return false;
        }
    }
};

160. Intersection of Two Linked Lists - Find a length of lists and compare between longer and shorter one from end

21. Merge Two Sorted Lists - use #2

141. Linked List Cycle - use #4

206. Reverse Linked List - use #3

234. Palindrome Linked List - use #3, 4(Find midpoint, reverse a list after midpoint, compare two lists)

142. Linked List Cycle II - use #4

876. Middle of the Linked List - use #4

19. Remove Nth Node From End of List - Find a previous pointer of target node and remove from a list

2. Add Two Numbers - use #2

0
Subscribe to my newsletter

Read articles from Hyunwoo Choi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hyunwoo Choi
Hyunwoo Choi