To be ashamed, this algorithm has stuck the author all afternoon. The reason is that there are problems in the understanding of continuous assignment of reference types. Therefore, I record this understanding process and hope that my knowledge thickness can increase by 1cm every day.

Let’s get to the point, leetcode-cn.com/problems/re…

🚀 comprehension:

  • For complex problems to learn to dismember, is to start from simple problems, find the same, first deal with a, and thenThe add traversal of the add traversal, the add judgment of the add judgment.

🚀 codePart:

var recoverLinkList = function(head){
  var obj1 = head;
  var obj2 = null;
  while(obj1){
    vartempVar = obj1.next; obj1.next = obj2; obj2 = obj1; obj1 = tempvar; }}Copy the code
  • Obj1 should point to NULL,obj2 to obj1, and then obj2 to obj1. Make the initial value of obj2 null

  • TempVar is a temporary variable set to hold the address to which obj1’s next points; This can also be obtained by the temporary variable tempVar on subsequent assignments to obj1.next to keep the loop going.

  • It is important to understand that the stack memory of a reference type holds an address to the heap memory. In other words, the heap memory is stored there, even if the pointer changes, it still exists there.