• 编程14:翻转单向和双向链表


    <?php
    header("content-type:text/html;charset=utf-8");
    /*
     *翻转单向和双向链表 P40
     */
    class SingleNode{
        public $value;
        public $next;
        public function __construct($value)
        {
            $this->value = $value;
        }
    }
    function reverseSingleList($head){
        if($head == null || $head->next == null){
            return $head;
        }
        $cur = $head;
        $pre = null;
        $next = null;
    
        while ($cur != null){
            $next = $cur->next;
            $cur->next = $pre;
            $pre = $cur;
            $cur = $next;
        }
        return $pre;
    }
    
    class DoubleNode{
        public $value;
        public $next;
        public $last;
        public function __construct($value)
        {
            $this->value = $value;
        }
    }
    
    function reverseDoubleList($head){
        if($head == null || $head->next == null){
            return $head;
        }
        $cur = $head;
        $pre = null;
        $next = null;
        while ($cur != null){
            $next = $cur->next;
            $cur->next = $pre;
            $cur->last = $next;
            $pre = $cur;
            $cur = $cur->next;
        }
        return $pre;
    }
    
    $head1 = new SingleNode(1);
    $head1->next = new SingleNode(3);
    $head1->next->next = new SingleNode(5);
    $head1->next->next->next = new SingleNode(7);
    $head1->next->next->next->next = new SingleNode(9);
    $head1->next->next->next->next->next = new SingleNode(11);
    
    echo "当前单链表为:";
    echo "</br>";
    print_r($head1);
    echo "</br>";
    echo "</br>";
    echo "翻转后的单链表为:";
    echo "</br>";
    print_r(reverseSingleList($head1));
    echo "</br>";
    echo "</br>";
    
    $head2 = new DoubleNode(1);
    $head2->last = null;
    $head2->next = new DoubleNode(3);
    $head2->next->last = $head2;
    $head2->next->next = new DoubleNode(5);
    $head2->next->next->last = $head2->next;
    $head2->next->next->next = new DoubleNode(7);
    $head2->next->next->next->last = $head2->next->next;
    $head2->next->next->next->next = null;
    
    
    echo "双向链表为:";
    echo "</br>";
    print_r($head2);
    echo "</br>";
    echo "</br>";
    echo "翻转后的双向链表为:";
    echo "</br>";
    print_r(reverseDoubleList($head2));

    实现结果:

  • 相关阅读:
    Linux环境缓存清理
    【算法】最小生成树Prime求解
    Linux配置tftp服务器用于局域网文件传输
    Linux Ubuntu安装Nvidia多GPU通信库NCCL
    基于 Docker 环境搭建 Zookeeper 集群
    Nacos 高可用环境搭建(基于Docker)
    bs::framework 编译注意事项
    2022/3/17 装机笔记克服重重困难,他亮了。
    c++ 知识回顾与复习以及历史c++语言版本总结
    c++ 中遇到的语法问题
  • 原文地址:https://www.cnblogs.com/xlzfdddd/p/10039154.html
Copyright © 2020-2023  润新知