Sword finger offer | interview question 15: delete the node of the linked list

Posted by davidcriniti on Tue, 04 Jan 2022 17:28:33 +0100

Knock algorithm series articles

  1. Ten classic sorting algorithms for dry goods and hand tearing
  2. Sword finger offer | understanding interview
  3. Sword finger offer | interview question 2: realizing Singleton mode
  4. Sword finger offer | interview question 3: finding two-dimensional array
  5. Sword finger offer | interview question 4: replace spaces
  6. Sword finger offer | interview question 5: print linked list from end to end
  7. Jianzhi offer | interview question 6: Rebuilding binary tree
  8. Sword finger offer | interview question 7: realizing queue with two stacks
  9. Sword finger offer | interview question 8: minimum number of rotation array
  10. Sword finger offer | interview question 9: Fibonacci sequence
  11. Sword finger offer | interview question 10: frog jumping steps
  12. Sword finger offer | interview question 11: matrix coverage
  13. Sword finger offer | interview question 12: the number of 1 in binary
  14. Sword finger offer | interview question 13: integer power of value
  15. Sword finger offer | interview question 14: print from 1 to the maximum n digits

"Leetcode : https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof

"GitHub : https://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_12_hammingWeight/Solution.java

Sword finger Offer 15 Delete the node of the linked list

Given the head pointer of the one-way linked list and the value of a node to be deleted, define a function to delete the node. Returns the head node of the deleted linked list.

Example 1:

input: head = [4,5,1,9], val = 5
 output: [4,1,9]
explain: Given the second node with a value of 5 in your linked list, after calling your function, the linked list should be 4 -> 1 -> 9.

Example 2:

input: head = [4,5,1,9], val = 1
 output: [4,5,9]
explain: Given the third node with a value of 1 in your linked list, after calling your function, the linked list should be 4 -> 5 -> 9.

Problem solving ideas:

There are two steps to delete the node with value val: locate the node and modify the reference.

  1. Locate node: traverse the linked list until head Jump out when Val = = Val to locate the target node.
  2. Modify reference: set the predecessor node of cur as pre and the successor node as cur next ; Execute pre next = cur.next, you can delete the cur node.

**Algorithm flow:**

  1. Special case handling: when the header node head should be deleted, it directly returns head Next.
  2. Initialization: pre = head, cur = head next .
  3. Locate node: jump out when cur is empty or cur node value is equal to val.
    1. Save the current node index, i.e. pre = cur.
    2. Traverse the next node, cur = cur next .
  4. Delete node: if cur points to a node, execute pre next = cur. next ; If cur points to nullnull, it means that the linked list does not contain a node with value val.
  5. Return value: return the head node of the linked list.

Complexity analysis:

  • Time complexity O(N): n is the length of the linked list. The deletion operation needs to cycle N/2 times on average and N times at worst.
  • Space complexity O(1): cur, pre occupy additional space of constant size.
package com.nateshao.sword_offer.topic_15_deleteNode;

/**
 * @date Created by Shao Tongjie on 2021/11/21 16:22
 * @WeChat official account programmers
 * @Personal website www.nateshao.com cn
 * @Blog https://nateshao.gitee.io
 * @GitHub https://github.com/nateshao
 * @Gitee https://gitee.com/nateshao
 * Description: Delete the node of the linked list
 */
public class Solution {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(3);
        int val = 3;
        ListNode node = deleteNode(listNode, val);
        System.out.println("node = " + node);
    }
 // recommend
    public static ListNode deleteNode(ListNode head, int val) {
        if (head.val == val) return head.next;
        ListNode pre = head, cur = head.next;
        while (cur != null && cur.val != val) {
            pre = cur;
            cur = cur.next;
        }
        if (cur != null) pre.next = cur.next;
        return head;
    }

    public void deleteNode(ListNode head, ListNode deListNode) {
        if (deListNode == null || head == null)
            return;
        if (head == deListNode) {
            head = null;
        } else {
            // If the deleted node is the end node, move it back one
            if (deListNode.next == null) {
                ListNode pointListNode = head;
                while (pointListNode.next.next != null) {
                    pointListNode = pointListNode.next;
                }
                pointListNode.next = null;
            } else {
                deListNode.val = deListNode.next.val;
                deListNode.next = deListNode.next.next;
            }
        }
    }

    /**
     * Single pointer implementation
     *
     * @param head
     * @param val
     * @return
     */
    public ListNode deleteNode2(ListNode head, int val) {
        if (head == null) return null;
        if (head.val == val) return head.next;
        ListNode cur = head;
        while (cur.next != null && cur.next.val != val) {
            cur = cur.next;
        }
        if (cur.next != null) {
            cur.next = cur.next.next;
        }
        return head;
    }

    /**
     * Recursive implementation
     *
     * @param head
     * @param val
     * @return
     */
    public ListNode deleteNode3(ListNode head, int val) {
        if (head == null) return null;
        if (head.val == val) return head.next;
        else head.next = deleteNode3(head.next, val);
        return head;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}

Reference link: https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/solution/mian-shi-ti-18-shan-chu-lian-biao-de-jie-dian-sh-2

The revolution has not yet succeeded, and comrades still need to work hard and rush