[Data Structure] that can take you off into the king Part 5: Chain surface test questions

content

Foreword:

Problem Description:

problem analysis:

Problem explanation:

Code:


Foreword:

Brothers, have a liver every day and feel refreshed! ! ! ! !

1. Chain surface test questions to determine whether the linked list is a palindrome.

Problem Description:

Brothers, look at the picture to understand what is the palindrome of a linked list:

Palindromic structure: read 12 -> 23 -> 34 forward, read 12 -> 23 -> 34 backward

Odd and even numbers are OK:

problem analysis:

To judge whether it is a palindrome structure, then we have to traverse the linked list, one from front to back, the other from back to front, the corresponding val values ​​should be the same, then we must modify the pointer of the linked list, here we will use the speed The pointer helps us find the middle node, change the pointer from the middle node, and start traversing after the change is completed.

Problem explanation:

Step 1: To ensure that our linked list can always be found, define a head variable that always points to the head node.

Step 2: Define two variables, both of which point to the head at the beginning, and find the intermediate node by means of the fast and slow pointer.

The third step: define a cur variable to point to the next node of the intermediate node, and let the cur variable to modify the pointer.

Step 4: Define a curNext variable equal to cur.Next to prevent the subsequent nodes from being found after changing the pointer.

Code:

  public boolean chkPalindrome(ListNode head) {
        if(head == null) return false;//判断一下链表是不是空,空的话直接返回false
        ListNode fast = head;//快指针fast,初始等于head
        ListNode slow = head;//慢指针slow,初始等于head
        while(fast != null && fast.next != null){//如果链表是奇数,fast.next == null停下,如果链表是偶数fast == null停下
            fast = fast.next.next;//fast走两步
            slow = slow.next;//slow走一步
 
        }
        ListNode cur = slow.next;//cur等于slow的下一个节点
        while(cur != null){//cur不为空开始反转
            ListNode curNext = cur.next;//curNext等于cur的下一个节点
            cur.next = slow;//开始反转
            slow = cur;//反转完了后让slow等于cur
            cur = curNext;//cur再往后走一步。
        }
        while(head != slow){//判断是不是回文结构
            if(head.val != slow.val){//不是回文结构
                return false;
            }
            if(head.next == slow){//偶数链表的情况
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
        
        
        
        
        
        
       
    }

Second, the chain surface test questions, input two linked lists, find their first common node.

Problem Description:

problem analysis:

judge:

1. If two linked lists are intersecting then is it Y or X shape? Y

2. If two linked lists intersect, the value val field is the same or the next field is the same? The next field is the same

The above figure is the intersecting linked list. 

Problem explanation:

Step 1: First define two byte variables that point to the heads of the two linked lists, headA and headB.

Step 2: Define two variables to find the difference between the two linked lists.

Step 3: Define two more byte variables ps and pl to point to headA and headB respectively.

Step 4: Let the long linked list take their difference steps first.

Step 5: The two linked lists go together.

Step 6: When ps=pl, it is a common node.

Code:

 public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headB == null || headA == null) return null;//判断其中一条链表的头节点为空就是没有焦点
        ListNode ps = headA;
        ListNode pl = headB;
        int lenA = 0;
        int lenB = 0;
        while(ps != null){求出ps链表的长度
            lenA++;
            ps = ps.next;
        }
        ps = headA;//让ps重新等于头节点
        while(pl != null){求出pl链表的长度
            lenB++;
            pl = pl.next;
        }
        pl = headB;//让pl重新等于头节点
        int len = lenA - lenB;
        if(len < 0){//判断ps长还是pl长
            ps = headB;
            pl = headA;
            len = lenB - lenA;
        }
         while(len != 0)//求两条链表的差值
                ps = ps.next;
                len--;

            }
        while(ps != pl){
            
            ps = ps.next;
            pl = pl.next;
        }
        return ps;



    }

Daily check-in: Currently, we are updating the linked list questions. If you are interested in this question, you can contact me privately. We can discuss and work hard together.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324125894&siteId=291194637