New study notes for breaking the linked list topic

之前专门学习了链表,很久没写了,最近在复习有一些新的心得...

1. Separate linked list

This shows that the original linked list has stickiness, so it is necessary to manually disconnect the next pointer of each node in the original linked list and continue to search

2. Merge k ascending linked lists

insert image description here

if(lists.length==0) return null;

This should be placed at the very beginning, otherwise this error will be reported (in fact, it is placed before the definition of the priority queue)

3. Find the kth node from the bottom of the linked list

class Solution {
    
    
    public ListNode getKthFromEnd(ListNode head, int k) {
    
    
        ListNode p1=head;
        ListNode p2=head;
        for(int i=0;i<k;i++){
    
    
            p1=p1.next;
        }
        while(p1!=null&&p1.next!=null){
    
    
            p2=p2.next;
            p1=p1.next;
        }
        return p2;
    }
}

If the example given is [1,2,3,4,5] 2 why would the output be 3-4-5

Because p1.next!=null will end early, that is, it will end before reaching the last node, then p2 will be one less p2=p2.next, so that it will be pushed back one less time

Of course, we can manually return p2.next after one less push

But this will cause [1] 1 to return an error

4. The middle node of the linked list

class Solution {
    
    
    public ListNode middleNode(ListNode head) {
    
    
        ListNode quick=head;
        ListNode slow=head;
        while(quick!=null&&quick.next!=null){
    
    
            quick=quick.next.next;
            slow=slow.next;
        }
        return slow;
    }
}

When I write this question myself, I will think about the end condition of the while loop for a long time

I wrote quick at the beginning! =null

This will lead to the following situation, an error occurs when running to quick=quick.next.next, because it returns to null when it reaches quick.next, and because it is null, it cannot continue to the second null, so it is will report an error

Focus on more specific situations when you have been scratching your head

This is when the quick is at 5, the slow pointer is at 3, then we have to let the while loop go through again to make the slow reach the position of 4 (why consider going again instead of returning slow.next, that is because in When the number of nodes in the linked list is odd, the quick goes to 5, and the slow pointer finds the midpoint, so just return slow, then we can’t judge whether it is odd or even, that is, we don’t know when to return slow and when to return slow.next, so I consider judging the condition so that there is no need to go again when it is odd, and it needs to go again when it is even)

Then, according to the manual movement of the pointer, the cycle condition for going one more time is to judge quick.next when the quick pointer is at 5! =null, so you can go one more time, only one time!

But what if the loop condition is just quick.next! =null, it will reach null because quick goes from 5 one more time, and null.next will report an error, so it needs to be in quick.next! =null before judging quick! =null

5. Ring linked list

In fact, I really want to find the middle node of the linked list, just check whether the fast and slow pointers meet in the loop

6. Ring linked list 2.0

insert image description here

The key to this question is to use distance analysis, and the code uses nodes to do it

Information that can be obtained from the above figure:

  1. The length of the ring: 2k-k (ps: it may be n times the length)
  2. Distance from the head node to the start of the ring: km
  3. Distance from the meeting point to the start of the ring: km

So slow can reach the ring node by taking km steps at the meeting point, and the cur pointer can reach the ring node by taking km steps at the head node

But in fact we don't know how much m is, so we learn from the idea of ​​finding the midpoint of the linked list, that is, the idea of ​​double pointers meeting

7. Intersecting linked list

Note that p1=p1.next and p2=p2.next must be in the else. If it is only in the while loop, it will cause the pointer to move backward every time, but in fact, it needs to be connected to another linked list. It is not needed here. move

Guess you like

Origin blog.csdn.net/weixin_52055811/article/details/131847169