LeetCode brush notes linked list traversal linked list

Keywords: C++ Algorithm data structure leetcode linked list

160 intersecting linked list

Given two linked lists, judge whether they intersect at a point, and find the intersection node.

The input is two linked lists and the output is one node. If there are no intersecting nodes, an empty node is returned.

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Intersected at '8'
Explanation: the value of intersection node is 8 (note that if two linked lists intersect, it cannot be 0). Starting from their respective headers, linked list A is [4,1,8,4,5], and linked list B is [5,0,1,8,4,5]. In A, there are 2 nodes before the intersection node; In B, there are 3 nodes before the intersection node.

Resolution:

Suppose the distance from the head node of linked list a to the intersection point is a, the distance from the head node of linked list b to the intersection point is b, and the distance from the intersection point to the end of the linked list is c.

We use two pointers to point to the head nodes of two linked lists respectively and advance at the same speed. If we reach the end of the linked list, we will move to the head node of another linked list and continue to advance. According to this forward method, the two pointers will reach the intersection node at the same time after a + b + c advances.

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode *pa = headA, *pb = headB;
        while(pa != pb){
            pa = pa?pa->next:headB;
            pb = pb?pb->next:headA;
        }
        return pa;
    }
};

234 palindrome linked list

Judge whether the linked list is palindrome with the spatial complexity of O(1).

The input is a linked list, and the output is a Boolean value indicating whether the linked list is palindrome.

Input: head = [1,2,2,1]
Output: true

Resolution:

  1. First use the speed pointer to find the midpoint of the linked list, and then cut the linked list in half

  2. Then turn the second half over, please refer to 206 reverse linked list

  3. Finally, two pointers are used to compare whether the two half elements are equal one by one

class Solution {
public:
    // Linked list flip
    ListNode* reverseList(ListNode* head) {
           ListNode *prev = nullptr, *next = head;
           while(head){
               next = head->next;
               head->next = prev;
               prev = head;
               head = next;
           }
           return prev;
       }

    bool isPalindrome(ListNode* head) {
        if(!head || !head->next){
            return true;
        }
        // The speed pointer finds the midpoint of the linked list
        ListNode *fast = head, *slow = head;
        while(fast->next && fast->next->next){
            fast = fast->next->next;
            slow = slow->next;
        }
        
        // Whether the length of the linked list is even or odd, slow points to the last node in the first half
        fast = slow->next;
        fast = reverseList(fast);
        slow = head;
        // Compare whether the elements of the two parts are consistent
        while(fast){
            if(slow->val != fast->val){
                return false;
            }
            slow = slow->next;
            fast = fast->next;
        }
        return true;
    }
};

19 delete the penultimate node of the linked list

Given a linked list, delete the penultimate node of the linked list, and return the head node of the linked list.

The input is a linked list, and the output deletes the linked list of the penultimate node

Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]

Resolution:

Like the idea of using the fast and slow pointer to find the midpoint of the linked list, let the fast pointer start n nodes before the slow pointer. Then when the fast pointer reaches the end of the linked list, the slow pointer is just at the penultimate node + 1 of the linked list, and its next node can be deleted.

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *slow=head, *fast=head;
        // Go n steps first
        for(int i=0;i<n;++i){
            fast=fast->next;
        }
        if(!fast){
            head = head->next;
            return head;
        }
        while(fast->next){
            fast=fast->next;
            slow=slow->next;
        }
        ListNode *delNode = slow->next;
        slow->next = delNode->next;
        delete delNode;
        return head;
    }
};

reference material

LeetCode 101: easily brush the title with you (C + +) Chapter 13: the linked list of three swordsmen

Posted by suvarthi on Wed, 10 Nov 2021 19:12:06 -0800