Leetcode Summary of endless series of linked list

Source: Internet
Author: User
Tags add numbers
Linked lists are very easy to get to in the interview, the general code is relatively short, but also to examine the interviewer's thinking comprehensiveness and the ability to write bug-free code. In the writing of the list of topics, it is recommended to draw a schematic diagram, and the head node, tail nodes of these special points taken into account. The main technique is to use dummy head, that is, in the chain table head plus a node point to head, so as to avoid the judgment of the head pointer, unified processing all cases, and finally return dummy->next; another one with dummy head equivalent method is to use the pointer **p to handle , also can achieve dummy head effect, than dummy good place is, do not delete dummy head, in addition if a node is larger, with **p also compared to save space, but logically than dummy to around a point.
Dummy Head Use example
listnode* processlist (listnode* head) {
    listnode* dummy = new ListNode ();
    Dummy->next = head;
    Process the list
    head = dummy->next;
    Delete dummy;//again, please remember delete
    return head; 
}
 
Pointer to pointer usage example
listnode* processlist (listnode* head) {
    listnode** pnode = &head;
    Process the list
    //each time the resulting pcur is connected to the new linked list Pnode
    return head;
}

List of frequently-used questions

Question one, given a single linked list, detect whether there is a ring.
Idea: (Fast and slow pointer) use two pointers p1,p2 from the list head to start traversing, p1 each step forward, P2 each advance two steps. If P2 arrives at the end of the list, indicating no ring, otherwise P1, P2 will inevitably meet at some point (P1==P2), thus detecting the link list in the ring.

The second, given two single-linked list (Head1, head2), detects whether two linked lists have intersections, if there is a return of the first intersection.
Idea: (two single-linked lists may intersect only "X" type, because the single-linked list has only one next pointer) if HEAD1==HEAD2, then obviously intersect, directly return to HEAD1. Otherwise, from head1,head2 start traversing two linked lists to get their length len1 and len2. Assuming len1>=len2, then the pointer p1 from head1 to move backward len1-len2 step. The pointer p2=head2, below P1, p2 each backward and compares p1p2 to be equal, if equal returns the node, otherwise two linked lists have no intersection.

Third, given a single linked list (head), if there is a ring, please return to the first node of the ring from the beginning.
Idea: Using problem one, we can check whether there are rings in the list. If there is a ring, then the P1P2 coincident point P must be in the ring. Disconnect the ring from P point by: P1=p, P2=p->next, P->next=null. At this point, the original single-linked list can be regarded as two single-linked list, one from the head, the other starting from the P2, so the use of two methods, we find their first intersection is the request.

Problem four, only to the order list of a node P (not the last node, that is, p->next!=null) pointer, delete the node.
Idea: The method is very simple, the first is to put the data in P, and then copy the P->next data into P, then delete P->next can.

Only one node p (non-null node) in the order list is inserted, and a node in front of P.
Idea: The method is similar to the former, first assign a node q, insert Q after P, then the data in P is copied into Q, and then the data to be inserted is recorded in P.

Topic six, given a single-linked header node, delete the list of the last K nodes.
Idea: Using two nodes P1,P2,P1 initialization point to the head node, P2 always points to the P1 after the K-node, two nodes parallel backward move until the P2 reaches the end of the list (NULL), and then delete the corresponding node according to P1.

Seven Complex linked list replication
Idea: A complex linked list whose nodes have a m_pnext pointer pointing to the next node, and a m_psibling that points to any node in the list or null. The first step is to create the corresponding n ', the n ' link after n, according to each node n of the original linked list, and the second step is to set the m_psibling of the nodes on the linked list; The third step is to split the long list into two: Linking the odd-numbered nodes is the original linked list, Linking the nodes of even locations is the list of duplicates.

Merge of 82 non-intersecting ordered linked lists
Idea: The main study of the merge process, and the operation of the list, in the Leetcode summary of endless series of the sort of relevant examples

Nine-link list rollover (including full flip, partial rollover, segmented flip) (recursive or non-recursive implementation)
Idea: The flip process typically requires three node pointers: the tail node Subtail (the first node to be flipped, the end of a new sequence after a step-by-step rollover), the pcur of the node to be flipped (actually the next node of the tail node), and the head node (each rollover needs to be updated). See also the following Leetcode example: Reverse Nodes in K-group, Reverse Linked List II

An algorithm for the order of List of problems
Idea: List sorting is available: merge, insert, bubble and select (one-way linked list), merge, Fast row, insert, bubble and select (doubly linked list), priority to merge, low complexity of time, simple to implement.

Topic 11 Deleting duplicate elements in an ordered single-linked list
Idea: There are two main ways to delete duplicates: one is to consider copying the value of useful nodes, overwriting the nodes that need to be deleted, and second, directly manipulating the list to delete or reorganizing with containers such as stacks or set.

Topic 12 simulating Large integer addition operations with a linked list
Idea: to pay attention to the comprehensive consideration of rounding

Summary of related topics on Leetcode

1.ADD Numbers

2.Swap Nodes in Pairs

3.Partition List

4.Remove Duplicates from Sorted List

5.Remove duplicates from Sorted List II

6.Remove Nth Node from End of List

7.Reverse Linked List II

8.Reverse Nodes in K-group

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.