anAlgoProbAday-linked_list_problems

  1. add_two_numbers_lists
    explained: 2 linked lists are added to calculate the value, such as 5 + 11 = 16, I feel useless.
  2. cloneListWithRandomPtr
    explained: There is a random pointer in the linked list. How to clone such a linked list is mainly divided into 2 steps. In the first step, the original linked list is doubled by inserting a new link, and then the extended linked list is separated into 2 linked lists , To achieve the purpose of cloning.
    The image is as follows:
    List in format (Node data, Random Node data):
    (1, 3) –> (2, 1) –> (3, 5) –> (4, 3) –> (5, 2) –> NULL
    List in format (Node data, Random Node data):
    (1, 3) –> (1, 3) –> (2, 1) –> (2, 1) –> (3, 5) –> (3 , 5) –> (4, 3) –> (4, 3) –> (5, 2) –> (5, 2) –> NULL
  3. deleteLinkedlist
    explained: it is to delete the linked list according to the rules, no.
  4. deleteNode
    explained: normal operation, point the pointer to the next linked list node. Then delete the current node.
  5. findIntersectionPointOfLists
    explained: it is to point the pointer of a linked list to another pointer, and then find the position where the two pointers coincide. It is generally easier to think about comparing the address of the pointer. In fact, it is enough to compare the length, only need to compare once.
  6. FloyedCycleDetection
    explained: Use the classic Frey's loop search algorithm to find the position where the loop starts. The method analogy: A runs at twice the speed of B, then A must enter the loop first. When A encounters B again, A compares B ran one more cycle, and if A was allowed to start walking at the same speed as B at this time, and B continued to walk, the point where A and B met again was the starting point of the cycle. You can write calculation verification. For example, x1 + x2 = a; x1 + x2 + l = 2a, that is x1 + x2 = l (l is the length of a cycle)
  7. insertInASortedLinkedList
    explained: It is not difficult to understand, it is to rearrange after comparing the size. This function is arranged when inserted. Kind of like sortedarray.
  8. listDemo
    explained: List encapsulates a linked list, Vector encapsulates an array, the main difference between List and Vector is that vector uses continuous memory storage, vector supports the [] operator, and List is implemented in the form of a linked list, does not support [], Vector The speed of random access is fast, but the insertion of elements in the head is very slow, and the tail is fast. List is much slower for random access, because it may have to traverse the entire linked list, but it is much faster to insert, no need to copy and move data, as long as you change the pointer. In addition, for newly added elements, vector has a set of algorithms, and List can be added arbitrarily.
  9. listPallindrome
    explained: using a strategy similar to that of FloatedCycleDetection, which is to divide the linked list first, then reverse the linked list, and then compare it. If there is different data, it is not a symmetric linked list.
  10. merge_sort
    explained: merge sort, has not completely absorbed the code. Worship this program. And achieve a return order, which is the most essential part of moveNode, its parameter type is * &, live so long for the first time see (their own low), look at the code, and is owned by no other sort Ten classic sort Algorithm (moving picture demo)
void moveNode( Node * & dstNode, Node * & srcNode )
{
  if (srcNode == nullptr) {
    return;
  }
  //srcNode的next赋值给nextNode
  Node * nextNode = srcNode->next;
  //把srcNode传入dummy,记住这里是dummy,下一步dstNode的地址和dummy就不一样了,因为这里传进来的参数是* &,既可以改变地址指向的值也可以改变地址
  dstNode->next = srcNode;
  //dstNode地址被改变
  dstNode = srcNode;
  //srcNode地址被改变,这里nextNode没有被delete,数量大了会不会有内存泄露啊!
  srcNode = nextNode;
}

What does * & here mean in C ++ when * and & mean? In fact, the role of this & is to reference, you can change the value of the pointer address.

  1. nthToLastNode
    Interpretation: Find the position of the penultimate node. After reading this, the principle of implementation is not difficult.
  2. printMiddleNode
    explained: nothing, just a fast and slow strategy.
  3. rearrange_list
    explained: nothing, if any, I think the highlight is in the reverse function, in fact, it is to separate the head node step by step, and then output it in reverse.
void reverse( Node * & head )
{
  Node * nextNode = nullptr;
  Node * newHead = nullptr;
  Node * curr = head;
  while( curr ) {
    nextNode = curr->next;
    curr->next = newHead;
    newHead = curr;
    curr = nextNode;
  }
  head = newHead;
}
  1. removeDuplicatesFromSortedList
    explained: when the comparison is not equal, reassign the address to head-next.
  2. reverseAlternateNodes
    explained: similar to merge_sort. Forget the meaning of * &, or I introduced it clearly. There should be nothing else.
  3. reverseLinkedListlterAndRecurse
    explained: reverseIter is relatively easy to understand, reverseRecur uses a nested way to flip, single-step debugging is roughly understood, it is not easy to understand, but there is nothing to achieve flip.
  4. SwapNodesWithoutSwappingData
    explained: through the swap node implementation, pre-process the linked list of the nodes before and after the node to be swapped, and then undergo a meal operation (to be further analyzed) to achieve node swapping.
Published 51 original articles · Like 13 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/windxf/article/details/104633397