To prove safety offer face questions 18 (java version): delete the list of nodes

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/littlehaes/article/details/91411211

welcome to my blog

To prove safety offer face questions 18 (java version): delete the list of nodes

Be sure to grasp the subject of two Advanced Code

Title II Advanced Code

Advanced topics two codes (recursive version)

A description of the subject

Deleted within O (1) time list node.
To order to the head pointer list and a pointer to a node, the node is removed to define a function in O (1) time list node is defined as follows:

 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

Thinking

  • Conventional ideas: To delete a node, so be abridged point before a node points to the next node to be abridged point
  • Looking for a node to be truncated prior to the point of the time complexity is O (n), is not satisfied O (1) requirements
  • Another idea: truncated point value to be changed to the next value to be abridged node points, then the next point to be truncated at a point of a node.
  • Above this idea does not require a node to find the point to be truncated, the time complexity is O (1)
  • Special list should be noted that: the end node; only one node in the linked list
  • To be abridged end point node, can only be found from the beginning of the end node on a node, the node point to null
  • When only one node list, directly to the head pointer to null

the complexity

  • Time complexity: O (1)
  • Space complexity: O (1)
public class MST18 {
    public static void main(String[] args) {

    }
    public static class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }
    }
    public static void deleteNode(ListNode pHead, ListNode pToBeDeleted)
    {
        // 健壮性判断
        if(pHead == null || pToBeDeleted == null)
            throw new RuntimeException("链表为空或未指定待删除节点");
        // 正常执行
        /*
        1. sp:链表只有一个节点, 头指针指向null即可
        2. sp:删除最后一个节点, 找出倒数第二个节点并让该节点指向null
        3. 令待删节点的value等于待删节点下一个节点的value, 并令待删节点指向下一个节点的下一个节点
         */
        //1.
        if(pHead.next == null)
            pHead = null;
        //2.
        ListNode temp = pHead;
        if(pToBeDeleted.next == null){
            while(temp.next != pToBeDeleted)
                temp = temp.next;
            temp.next = null;
        }
        //3.
        temp = pToBeDeleted.next;
        pToBeDeleted.val = temp.val;
        pToBeDeleted.next = temp.next;
    }
}

Topic two Description

In a sorted linked list nodes duplicate, delete the duplicate node list, the node does not retain repeated, returns the head pointer list. For example, the list 1-> 2-> 3-> 3-> 4-> 4-> 5 is treated 1-> 2-> 5

Thinking

  • Conditional too much trial and error several times before they succeed, it will be able to complete the combination of conditions when thinking about how to issue a temporary understanding is:!? Consider the conventional case, taking into account all the special circumstances

reward

  • What while conditions for using the list, it can be like this: PCurr = null; this while loop will handle all the nodes?!
  • A plurality of different combinations if conditions
  • The list in question, when used pCurr.next, most need to determine whether the null pCurr.next
/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public static ListNode deleteDuplication(ListNode pHead) {
        /*
        初始化pPre指向pHead, pCurr指向pHead.next, 重复标志位duplicated, 初始值false,表示当前没有重复
        sp1:链表只有一个节点, 直接return Head
        sp2:
        1.
        */
        // valid check
        if (pHead == null)
            return null;
        // sp1
        if (pHead.next == null)
            return pHead;
        // normally execution
        ListNode pPre = pHead;
        ListNode pCurr = pHead.next;
        boolean duplicated = false;
        // ensure pHead.val != pHead.next.val
        while (pHead != null) {
            if (pHead.next != null && pHead.val == pHead.next.val) {
                duplicated = true;
                pHead = pHead.next;
            } else if (pHead.next != null && pHead.val != pHead.next.val && duplicated) {
                pHead = pHead.next;
                duplicated = false;
            }
            else if (pHead.next != null && pHead.val != pHead.next.val)
                break;
            else if (pHead.next == null && !duplicated)
                break;
            else if(pHead.next == null )
                pHead = null;

        }

            //
            while (pCurr != null) { // 使用该条件确保while循环能够处理到最后一个节点
                if (pCurr.next == null && duplicated) {
                    pPre.next = null;
                    pCurr = null;
                } else if (pCurr.next == null && !duplicated) {
                    pPre.next = pCurr;
                    pCurr = null;
                }
                // 执行到这里, pCurr.next != null
                else if (pCurr.val == pCurr.next.val) {
                    duplicated = true;
                    pCurr = pCurr.next;
                } else if (pCurr.val != pCurr.next.val && duplicated) {
                    duplicated = false;
                    pCurr = pCurr.next;
                } else if (pCurr.val != pCurr.next.val && !duplicated) {
                    pPre.next = pCurr; // 删除重复节点
                    pPre = pCurr;
                    pCurr = pCurr.next;
                }
            }
        return pHead;
    }
}

Title II Advanced Code

Thinking

  • A pointer to a node, if this node is the val, then have determined whether the current node is null; if not applicable val present node, then it is not determined whether the current node is null
  • How to solve the first node to repeat the question? Create a node head, let head.next = pHead, so that in the implementation of the main function, if pHead is repeated node, head.next will point to the first non-overlapping nodes or points to the end of null
  • Null or not to deal with the end of the list alone
  • Judgment duplicate nodes, it is best not to judge the value of the adjacent node is the same, it is best to create a new node pNext, pNext move backward until the value is not equal to pCurr pNext
  • pPre certainly slower than pCurr, pPre.next will point to a non-repeating pCurr. So explain the list is processed when pCurr == null! carefully understand the phrase
  • It should always be clear when will the change point pPre.next! If and only if pPre.val! = PCurr.val while pCurr.val! = PNext.val
public class Solution {
    public ListNode deleteDuplication(ListNode pHead)
    {
        // input check
        if(pHead == null)
            return null;
        // execute
        ListNode head = new ListNode(0); //head在pHead前面, 如果LinkList全是重复的,则head指向null; 否则指向pHead
        head.next = pHead;
        ListNode pPre = head; // pPre一定比pCurr慢
        ListNode pCurr = pHead;
        
        while(pCurr!=null){
            int val = pCurr.val;
            ListNode pNext = pCurr.next; 
            if(pNext != null && pNext.val == val){
                while(pNext != null && pNext.val == val){
                    pNext = pNext.next;
                }
                pPre.next = pNext;
                pCurr = pNext;
            }
            else{
                pPre.next = pCurr;
                pPre = pCurr;
                pCurr = pCurr.next;
            }
        }
        return head.next;
    }
}

Advanced topics two codes (recursive version)

public class Solution {
    public ListNode deleteDuplication(ListNode pHead)
    {
        // 递归版本. 功能: 从当前节点开始,找到下一个非重复节点并返回;  递归终止条件:当前节点是链表末尾null或者当前节点是链表最后一个节点
        // input check
        if(pHead == null)
            return null;
        if(pHead.next == null)
            return pHead;
        // 执行到这里, 链表至少两个节点
        // 判断节点是否重复,需要用到节点的值.   相邻两节点值的关系: 相等, 不相等, 所以用if else就能将这两大类情况考虑完备
        ListNode pCurr = pHead.next;
        if(pCurr.val == pHead.val){
            while(pCurr != null && pCurr.val == pHead.val){
                pCurr = pCurr.next;
            }
            // while循环外, pCurr可能是null, 也可能不是null
            // 继续递归
            // 是null的话,下一次递归返回null,返回的null将会被上一个非重复节点所指向
            // 不是null的话, 下一次继续递归将会继续从当前节点开始寻找下一个非重复节点
            return deleteDuplication(pCurr); // 弄清楚返回给谁!
        }
        else{ // pHead.next.val != pHead.val
            pHead.next = deleteDuplication(pCurr);
            return pHead;
        }
    }
}

Guess you like

Origin blog.csdn.net/littlehaes/article/details/91411211