Leetcode 237. Delete Node in a Linked List-删除链表的指定节点,不给链表的头节点

Posted 二十六画生的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 237. Delete Node in a Linked List-删除链表的指定节点,不给链表的头节点相关的知识,希望对你有一定的参考价值。

Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.

It is guaranteed that the node to be deleted is not a tail node in the list.

Example 1:

Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.

Example 2:

Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.

Constraints:

  • The number of the nodes in the given list is in the range [2, 1000].
  • -1000 <= Node.val <= 1000
  • The value of each node in the list is unique.
  • The node to be deleted is in the list and is not a tail node
package com.linkedlist;

/**
 * @Author you guess
 * @Date 2022/4/5 09:47
 * @Version 1.0
 * @Desc
 */
public class Leetcode_237_DeleteNodeinaLinkedList 


    /**
     * 方法1
     * T = O(n)
     * <p>
     * Runtime: 0 ms, faster than 100.00% of Java online submissions for Delete Node in a Linked List.
     * Memory Usage: 44.4 MB, less than 17.25% of Java online submissions for Delete Node in a Linked List.
     *
     * @param node
     */
    public void deleteNode1(ListNode node) 
        while (node != null && node.next != null) 
            node.val = node.next.val;
            if (node.next.next == null) //node指向倒数第2个节点时则可以设置node.next=null,则可以退出while了
                node.next = null;
                break;
            
            node = node.next;
        
    


    /**
     * 方法2 太棒了!太棒了!太棒了!
     * 不用遍历,直接指向下一个节点
     * T = O(1)
     * Runtime: 0 ms, faster than 100.00% of Java online submissions for Delete Node in a Linked List.
     * Memory Usage: 44.2 MB, less than 28.50% of Java online submissions for Delete Node in a Linked List.
     *
     * @param node
     */
    public void deleteNode(ListNode node) 
        node.val = node.next.val; //copy the next node value to curr
        node.next = node.next.next; // delete the node
    


    /**
     * 方法3:反转时把链表搞断了
     * Wrong Answer
     * Input
     * [4,5,1,9],5
     * Output
     * [4,5]
     * Expected
     * [4,1,9]
     *
     * @param head
     * @param node
     */
    public void deleteNodeWrong(ListNode head, ListNode node) 
        ListNode reverseHead = this.reverseLinkedList(node);
        ListNode reverseHeadOld = reverseHead;
        ListNode reverseHeadPre = null;
        while (reverseHead != node) 
            reverseHeadPre = reverseHead;
            reverseHead = reverseHead.next;
        
        reverseHeadPre.next = null;
        ListNode temp = this.reverseLinkedList(reverseHeadOld);

        printListNode(head);
    


    /**
     * 反转链表
     * 3 2 1 -> 1 2 3
     * Runtime: 6 ms, faster than 73.89% of Java online submissions for Palindrome Linked List.
     * Memory Usage: 97.4 MB, less than 57.01% of Java online submissions for Palindrome Linked List.
     *
     * @param head
     * @return
     */
    ListNode reverseLinkedList(ListNode head) 
        //无节点 或 只有 1个节点
        if (head == null || head.next == null) 
            return head;
        

        ListNode pre = null;
        ListNode nextTemp = null;
        ListNode cur = head;
        while (cur != null) 
            nextTemp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = nextTemp;
        
        return pre;
    


    public void printListNode(ListNode head) 
        while (head != null) 
            System.out.println(head.val);
            head = head.next;
        
    


    public static void main(String[] args) 
        ListNode node41 = new ListNode(9, null);
        ListNode node31 = new ListNode(1, node41);
        ListNode node21 = new ListNode(5, node31);
        ListNode node11 = new ListNode(4, node21);

        Leetcode_237_DeleteNodeinaLinkedList main = new Leetcode_237_DeleteNodeinaLinkedList();
        main.deleteNodeWrong(node11, node21);
        //main.deleteNode(node21);
    

方法2:

方法3: 反转时把链表搞断了

以上是关于Leetcode 237. Delete Node in a Linked List-删除链表的指定节点,不给链表的头节点的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode笔记:237. Delete Node in a Linked List

LeetCode237-delete-node-in-a-linked-list

[LeetCode]237. Delete Node in a Linked List

Leetcode237. Delete Node in a Linked List

[LeetCode] 237. Delete Node in a Linked List

LeetCode OJ 237Delete Node in a Linked List