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));
  • 相关阅读:
    腾讯广告算法大赛2019
    Mysql的部分常用SQL语句
    org.activiti.dependencies 7.1.0.M6 造成版本冲突问题的解决
    windows 将 redis 注册为服务 自动启动或手动启动
    org.springframework.security.access.AccessDeniedException: Access is denied
    对两个List进行关联匹配,选择匹配上的记录形成新的List输出
    越是大型的组织,越需要试验基地,试验基地应有特殊待遇
    dubbo+zookeeper 安装所遇系列问题
    签名与验签——图解
    关于航空母舰战斗群出海训练,常有大量鱼群跟随问题的建议和设想
  • 原文地址:https://www.cnblogs.com/xlzfdddd/p/10162809.html
Copyright © 2011-2022 走看看