20200908: On the collection of linked list questions

On the collection of linked list topics

topic

1. Likou 160. Intersecting Linked List
Insert picture description here
2.141. Ring Linked List
Insert picture description here
3.142. Ring Linked List II
Insert picture description here
4. 86. Separating Linked List

Insert picture description here

Ideas and algorithms

  1. 160 Intersecting linked lists: Two methods, the intuitive is to store the value of the set, store the pointer of a table in the set, and then traverse the other table. If the same set is stored, then the point is the intersection. Or use another method to align the long and short lists, align the tails of the long and short lists, and move the head to the alignment, and then start to synchronize and move the pointer. When the pointer points to the same node, it is the intersection Point, this question is very simple, but also very basic, pay attention to understanding.
  2. The solutions of 141 and 142 are basically the same. We only need to use the idea of ​​set in the previous question, and add nodes to the set in turn. If head already appears in the set, return head. 141 returns true, and 142 returns to the current node. The idea is the same.
  3. The question of 86 separated linked list is mainly to find two dummy heads and lead the corresponding nodes away respectively. The one larger than x is followed by a dummy head, and the one smaller than the other one, and finally connected.

Code

160 intersecting linked list:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    
    
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    
    
        HashSet<ListNode> set = new HashSet<>();
        while(headA != null){
    
    
            set.add(headA);
            headA = headA.next;
        }
        while (headB != null) {
    
    
            if (set.contains(headB)) {
    
    
                return headB;
            }
            headB = headB.next;
        }
        return null;
    }
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    
    
        // 新建空指针
        ListNode *head = NULL;
        // 先对齐两个链表
        int lenA = getLength(headA);
        int lenB = getLength(headB);
        if (lenA >= lenB) {
    
    
            headA = Formulation(lenA,lenB,headA);
        } else {
    
    
            headB = Formulation(lenB,lenA,headB);
        }
        // 再同时移动指针直到指向的地址相同为止,此时返回结果即可
        while(headA && headB) {
    
    
            if (headA == headB) {
    
    
                return headA;
            }
            headA = headA->next;
            headB = headB->next;
        }
        return NULL;
    }

    // 计算链表长度
    int getLength(ListNode *head) {
    
    
        int res = 0;
        while(head) {
    
    
            res++;
            head = head->next;
        }
        return res;
    }

    // 让长链表与短链表对齐
    ListNode *Formulation(int long_len,int short_len,ListNode *head) {
    
    
        int differ = long_len - short_len;
        while(differ && head) {
    
    
            head = head->next;
            differ--;
        }
        return head;
    }
};
  1. Circular linked list
/**
 * 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) {
    
    
        std::set<ListNode*> node_set;
        while(head) {
    
    
            if(node_set.find(head) != node_set.end()) {
    
    
                return true;
            }
            node_set.insert(head);
            head = head->next;
        }
        return false;
    }
};
  1. Circular Linked List II
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode *detectCycle(ListNode *head) {
    
    
        std::set<ListNode*> node_set;
        while(head) {
    
    
            if(node_set.find(head) != node_set.end()) {
    
    
                return head;
            }
            node_set.insert(head);
            head = head->next;
        }
        return NULL;
    }
};

86 separated linked list

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* partition(ListNode* head, int x) {
    
    
        // 临时的两个头结点并新建两个指针指向这两个头结点
        ListNode l_head(0);
        ListNode m_head(0);
        ListNode* l_ptr = &l_head;
        ListNode* m_ptr = &m_head;
        // 遍历,把比x大的节点放在m_head之后,小的放在l_head之后
        while(head){
    
    
            if(head->val >= x) {
    
    
                m_ptr->next = head;
                m_ptr = head;
            } else {
    
    
                l_ptr->next = head;
                l_ptr = head;
            }
            head = head->next;
        }
        l_ptr->next = m_head.next;
        m_ptr->next = NULL;
        return l_head.next;
    }
};

Write at the end

Rush!

Guess you like

Origin blog.csdn.net/qq_36828395/article/details/108479933