linked list of data structures

The linked list should be the most frequently mentioned data structure in the interview. The structure of the linked list is very simple. It connects several nodes into a chain structure by pointers.
Operations such as creating a linked list, inserting a node, and deleting a node can be implemented with only about 20 lines of code, so it is suitable for questions during interviews.
Common interview questions are:
1) Print the linked list from the end to the beginning;
2) Delete the node of the linked list;
3) Delete the k-th last node in the linked list;
4) Reverse
the linked list; 5) Merge two sorted linked lists;
6) The first common node of the two linked lists;
7) Determine whether there is a cycle in the linked list;
8) Determine whether a linked list is a palindrome;

The corresponding problem-solving ideas are:
1) First, print the linked list from the beginning to the end, put the printed value into a stack, and then pop the elements from the stack in turn. The last-in-first-out structure of the stack is used to print the linked list from the end to the beginning.
2) Input: Assign the value of the node to be deleted as
the value of the next node, and then let it point to the next node.

Node.val=Node.next.val
Node.next=Node.next.next

That is, assign the next node to this node to realize the operation of deleting this node.
3) In the linked list, because the memory is not continuous, an auxiliary pointer is needed to indicate the position of the node.
For example, if you want to find the k-th last node, then two pointers are used. Fast is at the first k positions of the slow pointer. When fast points to none, slow points to the last k-th node.

class Solution(object):
    def removeNthFromEnd(self, head, n):
        slow=fast=dummy=ListNode(0)
        dummy.next=head
        for i in range(n+1):
            fast=fast.next
        while fast:
            slow=slow.next
            fast=fast.next
        slow.next=slow.next.next
        return dummy.next       #考虑删除的是head的情况  

4) Using the iterative method, save the next node in turn -> let this node point to the previous node -> continue to traverse the next saved node.

pre=None
while head:
        Next=head.next
        head.next=pre
        pre=head
        head=Next
    return pre

5) Using a recursive method, compare the first two nodes of the linked list each time, and connect the smaller value to the back, and then compare the following nodes in turn.

if not l1:
    return l2
if not l2:
    return l1
if l1.val>l2.val:
    a,b=l2,l1
else:
    a,b=l1,l2
a.next=self.sort(a.next,b)
return a

Thinking: What if two sorted arrays are merged? The space complexity is required to be O(1)

When merging two arrays, if the magnitude of each digit (character) from the front to the back needs to move the number (character) multiple times, then we can consider the magnitude from the back to the front, which can reduce the number of moves, thus Improve efficiency.
6) When two singly linked lists have a common node, all the nodes behind them are the same. Therefore, if we can start the comparison from the last data and find the last identical node is the first common node, we can use the stack to store the nodes of the two linked lists, but this requires O(M+N) space the complexity. An important reason for not being able to compare from scratch is that the lengths of the two linked lists are not the same. Therefore, you can first traverse the two linked lists to get their lengths, and then you can know which linked list is longer, and how much longer the long linked list is than the short linked list; in the second traversal, let the long linked list take a few more steps, and then Traverse the two linked lists at the same time, and the first identical node found is their first common node.

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        if not headA or not headB:
            return None
        a,b=headA,headB
        while a is not b:
            a=a.next if a else headB  #注意这里是if a而不是a.next
            b=b.next if b else headA
        return a

In order to consider the case where there is no common node between the two, the case of a=b=None also exists in the second traversal.
7) From a novel point of view, if there is a ring in the linked list, then two people run in the ring at different speeds, and they will meet one day.

try:
    slow=head
    fast=head.next   #一开始就设置slow和fast的步长不同,好处是在输入为[]时fast报错,否则两者相等。
    if slow is not fast:
        slow=slow.next
        fast=fast.next.next
    return True
except AttributeError as e:
    return False    

8) The nature of the palindrome is that reading from front to back is the same as reading from back to front, that is, the value of the linked list is symmetric, then the first half of the linked list can be reversed, and then the reversed part and the second half of the value are compared. Consistent.

class Solution(object):
    def isPalindrome(self, head): 
        pre=None
        slow=fast=head
        while fast and fast.next:
            fast=fast.next.next
            Next=head.next
            head.next=pre
            pre=head
            head=Next
        if fast:
            head=head.next
        while pre and pre.val == head.val:
            pre=pre.next
            head=head.next
        if not pre and not head:
            return True
        else:
            return False

Guess you like

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