zoukankan      html  css  js  c++  java
  • 面试题18:删除链表节点

    18.1 在 O(1) 时间内删除链表节点

    <?php
    header("content-type:text/html;charset=utf-8");
    /*
     * 在 O(1) 时间内删除链表节点。 P119
     */
    class ListNode{
        var $val;
        var $next = NULL;
        function __construct($x){
            $this->val = $x;
        }
    }
    function deleteNode($head,$deleteNode){
        if($head == null || $deleteNode == null){
            return false;
        }
        if($head->next == null){
            return null;
        }
        if($deleteNode->next == null){
            return traverseDelete($head);
        }
        else{
            $deleteNode->val = $deleteNode->next->val;
            $deleteNode->next = $deleteNode->next->next;
            return $head;
        }
    }
    
    function traverseDelete($head){
        $cur = $head;
        while ($cur->next->next != null){
            $cur = $cur->next;
    
        }
        $cur->next = null;
        return $head;
    }
    
    $head = new ListNode(1);
    $head->next = new ListNode(2);
    $head->next->next = new ListNode(3);
    $head->next->next->next = new ListNode(4);
    $head->next->next->next->next = new ListNode(5);
    $deleteNode = $head->next->next->next->next;
    print_r(deleteNode($head,$deleteNode));

    18.2 删除链表中重复的结点

    NowCoder

    <?php
    header("content-type:text/html;charset=utf-8");
    /*
     * 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
     * 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5。P122
     */
    class ListNode{
        var $val;
        var $next = NULL;
        function __construct($x){
            $this->val = $x;
        }
    }
    function deleteDuplication($pHead)
    {
        if($pHead == null || $pHead->next == null){
            return $pHead;
        }
    
        $next = $pHead->next;
    
        if($pHead->val == $next->val){  //若头结点与相邻的节点相等,就一直向下遍历到不等的地方,此时next就指向不等的结点
            while ($next != null && $pHead->val == $next->val){
                $next = $next->next;
            }
            return deleteDuplication($next); //以该不等的结点为头,继续向下遍历
        }
        else{
            $pHead->next = deleteDuplication($pHead->next);//若头结点与相邻的节点不等,那就看下一个结点。
        }
    
        return $pHead;
    
    }
    
    $head = new ListNode(1);
    $head->next = new ListNode(1);
    $head->next->next = new ListNode(2);
    $head->next->next->next = new ListNode(3);
    $head->next->next->next->next = new ListNode(3);
    $head->next->next->next->next->next = new ListNode(4);
    $head->next->next->next->next->next->next = new ListNode(5);
    print_r(deleteDuplication($head));
  • 相关阅读:
    《JFlow: Practical Mostly-Static Information Flow Control》
    《嵌入式Linux C编程》第一章笔记
    Ansible --- 通过Ansible管理地区机房中的内网机器
    等保审核 --- MySQL密码复杂度
    等保审核 --- MySQL连接控制插件
    等保审核 --- MySQL操作审计记录
    CSS中居中的完全指南(中英对照翻译)
    svn提交报database is locked
    PHP session_cache_expire 会话函数
    MySQL CONCAT_WS 函数
  • 原文地址:https://www.cnblogs.com/xlzfdddd/p/10162809.html
Copyright © 2011-2022 走看看