Home  >  Article  >  Backend Development  >  How to reverse linked list in php (code example)

How to reverse linked list in php (code example)

不言
不言Original
2018-09-12 17:17:422846browse

The content of this article is about how to implement reverse linked list (code example) in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Common methods are divided into iteration and recursion. Iteration is from beginning to end, and recursion is from end to beginning.
2. Set two pointers, old and new, and add each item after new. The new linked list head pointer points to the new linked list head
3. old->next cannot directly point to new, but a temporary pointer tmp should be set to point to the address space pointed by old->next, save the original linked list data, and then old->next points to new, new moves forward to old, new=old, and finally old=tmp retrieves the data

while(old!=null){
  tmp=old->next
  old->next=new
  new=old
  old=tmp
}
<?php
class Node{
        public $data;
        public $next;
}
//头插法创建一个链表
$linkList=new Node();
$linkList->next=null;//头结点
for($i=1;$i<=10;$i++){
        $node=new Node();
        $node->data="aaa{$i}";//创建新结点$node
        $node->next=$linkList->next;//$node->next指向头结点->next
        $linkList->next=$node;//头结点->next指向$node
}

var_dump($linkList);


function ReverseList($pHead){
        $old=$pHead->next;//跳过头结点
        $new=null;
        $tmp=null;
        //反转过程
        while($old!=null){
                $tmp=$old->next;
                $old->next=$new;
                $new=$old;
                $old=$tmp;
        }   
        //给新链表加个头结点
        $newHead=new Node();
        $newHead->next=$new;
        var_dump($newHead);
}
ReverseList($linkList);
object(Node)#1 (2) {
  ["data"]=>
  NULL
  ["next"]=>
  object(Node)#11 (2) {
    ["data"]=>
    string(5) "aaa10"
    ["next"]=>
    object(Node)#10 (2) {
      ["data"]=>
      string(4) "aaa9"
      ["next"]=>
      object(Node)#9 (2) {
        ["data"]=>
        string(4) "aaa8"
        ["next"]=>
        object(Node)#8 (2) {
          ["data"]=>
          string(4) "aaa7"
          ["next"]=>
          object(Node)#7 (2) {
            ["data"]=>
            string(4) "aaa6"
            ["next"]=>
            object(Node)#6 (2) {
              ["data"]=>
              string(4) "aaa5"
              ["next"]=>
              object(Node)#5 (2) {
                ["data"]=>
                string(4) "aaa4"
                ["next"]=>
                object(Node)#4 (2) {
                  ["data"]=>
                  string(4) "aaa3"
                  ["next"]=>
                  object(Node)#3 (2) {
                    ["data"]=>
                    string(4) "aaa2"
                    ["next"]=>
                    object(Node)#2 (2) {
                      ["data"]=>
                      string(4) "aaa1"
                      ["next"]=>
                      NULL
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
object(Node)#12 (2) {
  ["data"]=>
  NULL
  ["next"]=>
  object(Node)#2 (2) {
    ["data"]=>
    string(4) "aaa1"
    ["next"]=>
    object(Node)#3 (2) {
      ["data"]=>
      string(4) "aaa2"
      ["next"]=>
      object(Node)#4 (2) {
        ["data"]=>
        string(4) "aaa3"
        ["next"]=>
        object(Node)#5 (2) {
          ["data"]=>
          string(4) "aaa4"
          ["next"]=>
          object(Node)#6 (2) {
            ["data"]=>
            string(4) "aaa5"
            ["next"]=>
            object(Node)#7 (2) {
              ["data"]=>
              string(4) "aaa6"
              ["next"]=>
              object(Node)#8 (2) {
                ["data"]=>
                string(4) "aaa7"
                ["next"]=>
                object(Node)#9 (2) {
                  ["data"]=>
                  string(4) "aaa8"
                  ["next"]=>
                  object(Node)#10 (2) {
                    ["data"]=>
                    string(4) "aaa9"
                    ["next"]=>
                    object(Node)#11 (2) {
                      ["data"]=>
                      string(5) "aaa10"
                      ["next"]=>
                      NULL
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Related recommendations:

About PHP How to implement the definition and reversal function of a linked list

php example code for implementing a singly linked list_PHP tutorial

The above is the detailed content of How to reverse linked list in php (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn