LeetCode237_Delete Node in a Linked List(删除链表中的节点) Java题解

题目:

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

解题:

今天早上无意间看到链表里还有一题easy的没有做,果断做了,看到题目之后,感觉这题有点像脑筋急转弯,它要求删除链表中的一个节点,而且只给那个节点,不给其他信息,另外还说了这个节点不会是最后一个节点。

解题思路就是,用节点的下一个节点的值覆盖要删除的那个节点,然后删除下一个节点,就这样。

代码:

public void deleteNode(ListNode node) {
		node.val=node.next.val;
		node.next=node.next.next;
        
    }


你可能感兴趣的:(LeetCode)