
Knock algorithm series articles
- Ten classic sorting algorithms for dry goods and hand tearing
- Sword finger offer | understanding interview
- Sword finger offer | interview question 2: realizing Singleton mode
- Sword finger offer | interview question 3: finding two-dimensional array
- Sword finger offer | interview question 4: replace spaces
- Sword finger offer | interview question 5: print linked list from end to end
- Jianzhi offer | interview question 6: Rebuilding binary tree
- Sword finger offer | interview question 7: realizing queue with two stacks
- Sword finger offer | interview question 8: minimum number of rotation array
- Sword finger offer | interview question 9: Fibonacci sequence
- Sword finger offer | interview question 10: frog jumping steps
- Sword finger offer | interview question 11: matrix coverage
- Sword finger offer | interview question 12: the number of 1 in binary
- Sword finger offer | interview question 13: integer power of value
- 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.
- Locate node: traverse the linked list until head Jump out when Val = = Val to locate the target node.
- 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:**
- Special case handling: when the header node head should be deleted, it directly returns head Next.
- Initialization: pre = head, cur = head next .
- Locate node: jump out when cur is empty or cur node value is equal to val.
- Save the current node index, i.e. pre = cur.
- Traverse the next node, cur = cur next .
- 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.
- 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