Linked list double pointer

Double pointer:

Find the starting node where two linked lists intersect.

Case 1: Two linked lists intersect

The lengths of linked lists headA and headB are m and n respectively. Suppose there are a nodes in the disjoint part of the linked list headA, b nodes in the disjoint part of the linked list headB, and c nodes in the intersecting part of the two linked lists, then a + c = m, b + c = n.

If a = b, the two pointers will reach the node where the two linked lists intersect at the same time, and return the intersected node at this time;

If a != b, the pointer pA will traverse the linked list headA, and the pointer pB will traverse the linked list headB. The head node of the linked list headA, and then the two pointers continue to move. After the pointer pA has moved a+c+ba+c+b times and the pointer pB has moved b+c+ab+c+a times, the two pointers will move simultaneously Arrive at the node where the two linked lists intersect, which is also the node pointed to by the two pointers at the same time for the first time, and return the intersected node at this time.

Case 2: Two linked lists are disjoint

The lengths of linked lists headA and headB are mm and nn respectively. Consider how the two pointers move when m = n and m != n:

If m = n, the two pointers will reach the end nodes of the two linked lists at the same time, and then become null at the same time, and return null at this time;

If m != n, since the two linked lists have no common nodes, the two pointers will not reach the end nodes of the two linked lists at the same time, so both pointers will traverse the two linked lists and move m + n times at the pointer pA , After the pointer pB moves n + m times, the two pointers will become null at the same time, and return null at this time.

The K-th last point in the linked list:

Define two pointers, let the first pointer go forward k - 1 steps, and then let the two pointers move forward at the same time, when the first pointer reaches the end node, the second pointer is because it is the same as the first There is a distance of k - 1 for each pointer, so at this time the second pointer is the kth node from the bottom.

Fast and slow pointer:

The fast and slow pointer means that there are two pointers, one fast pointer and one slow pointer. The two pointers move at different speeds each time, the fast one moves fast, and the slow one moves slowly. The speed in the speed pointer refers to the moving step, that is, the speed of moving forward each time. For example, the fast pointer can be moved forward 2 times along the linked list each time, and the slow pointer can be moved forward 1 time each time.

application:

The inability to efficiently obtain the length and the inability to quickly access elements based on offsets are two disadvantages of linked lists. However, during the interview, we often encounter problems related to the length and position, such as obtaining the penultimate k-th element, obtaining the element in the middle position, judging whether there is a ring in the linked list, and judging the length of the ring. These problems can be solved by using double pointers flexibly.

Determine whether a singly linked list has a cycle:

Define two pointers, one fast pointer and one slow pointer. The fast pointer advances two steps each time, and the slow pointer advances one step each time. If the fast pointer catches up with the slow pointer, it means that the linked list is a circular linked list. If not, then Prove that this linked list is a singly linked list.

PS. If there is a ring, how to judge the length of the ring? The method is that after the fast and slow pointers meet, they continue to move until they meet for the second time. The number of moves between encounters is the length of the loop.

Find the median in a sorted list:

Define two pointers, one fast pointer and one slow pointer. Because the linked list is an ordered linked list, we control the fast pointer to advance two steps each time, and the slow pointer to advance one step. When the fast pointer reaches the end node of the linked list, the slow pointer will go. We have reached the middle of the linked list. In addition, there is another trap in this question: judge the parity of the number of linked list nodes. If our fast pointer moves x times, if the number of times it reaches the end of the table is (1 + 2x), it proves that the number of nodes is odd, then the value of the slow pointer at this time is the median. If the penultimate node of the linked list is reached, it proves that the number of nodes is even. Returns the average value of the current value of the slow pointer and the next value.
 

Guess you like

Origin blog.csdn.net/qq_51588894/article/details/129246676