美文网首页
单链表反转

单链表反转

作者: 淡淡de盐 | 来源:发表于2020-08-25 11:05 被阅读0次

    单链表


    单链表反转

    function reversal()
    {
        if ($this->size == 0) {
            return $this->header;
        }
    
        $pre     = null;
        $current = $this->header;
        while ($current != null) {
            $tmp = $current->next;
    
            $current->next = $pre;
    
            $pre = $current;
    
            $current = $tmp;
        }
        $this->header = $pre;
    
        return $this;
    }
    

    递归方法

    public function reversRecursionNode($head)
    {
        if ($head->next == null) {
            return $head;
        }
        $phead            = $this->reversRecursionNode($head->next);
        $head->next->next = $head;
        $head->next       = null;
    
        return $phead;
    }
    

    相关文章

      网友评论

          本文标题:单链表反转

          本文链接:https://www.haomeiwen.com/subject/jebajktx.html