• php链表笔记:单链表反转


    <?php
    /**
     * Created by PhpStorm.
     * User: huizhou
     * Date: 2018/12/1
     * Time: 11:41
     */
    
    /**
     * 1.链表的反转
     * Class Node
     */
    class Node
    {
        private $value;
        private $next;
    
        public function __construct($value = null)
        {
            $this->value = $value;
        }
    
        public function getValue()
        {
            return $this->value;
        }
    
        public function setValue($value)
        {
            $this->value = $value;
        }
    
         public function getNext()
        {
            return $this->next;
        }
    
        public function setNext($next)
        {
            $this->next = $next;
        }
    }
    
       // 遍历方式,将当前节点的下一个节点缓存后更改成当前节点指针
        function reverse(Node $head){
    
           if($head == null){
               return $head;
           }
    
           $pre = $head; // 取出head节点
           $cur = $head->getNext(); // 把当前节点指向下一个节点
    
           $next = null;
           while($cur != null){
               $next = $cur->getNext();
               $cur->setNext($pre); // 把当前节点的指针指向前一个节点
               $pre = $cur;
               $cur = $next;
           }
    
           // 将原链表的头节点的下一个节点设置为null,再把反转后的头节点赋给head
           $head->setNext(null);
           $head = $pre;
    
           return $head;
        }
    
        // 递归实现,在反转当前节点之前先反转后续节点
        function reverse2(Node $head){
           if($head == null || $head->getNext() == null){
               return $head;
           }
    
           $reversedHead = reverse2($head->getNext());
           $head->getNext()->setNext($head);
           $head->setNext(null);
    
           return $reversedHead;
        }
    
        function test(){
           $head = new Node(0);
           $tmp = null;
           $cur = null;
    
           // 构造一个长度为10的链表,保存头节点对象head
            for ($i = 1;$i < 10 ; $i++){
                $tmp = new Node($i);
                if ($i == 1){
                    $head->setNext($tmp);
                }else{
                    $cur->setNext($tmp);
                }
                $cur = $tmp;
            }
    
            $tmpHead = $head;
            while ($tmpHead != null){
                echo $tmpHead->getValue();
                $tmpHead = $tmpHead->getNext();
            }
    
            echo "
    ";
    
            $head = reverse2($head);
    
            while ($head != null ){
                echo $head->getValue();
                $head = $head->getNext();
            }
        }
    
        test();
  • 相关阅读:
    Linux操作_常用命令操作练习
    Linux编程_Shell脚本练习题
    Linux操作_grep/egrep工具的使用
    Linux中的链接文件_软链接和硬链接
    Linux操作_磁盘管理_增加虚拟磁盘
    Linux命令_磁盘管理_查看磁盘或目录的容量
    Linux命令_用户身份切换
    使用Unity中的Box Collider组件完成游戏场景中的碰撞检测功能
    在Unity场景中更改天空盒的步骤
    Linux命令_用户和用户组管理
  • 原文地址:https://www.cnblogs.com/mrszhou/p/10053307.html
Copyright © 2020-2023  润新知