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;

    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 08:39:35

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

(LeetCode)Delete Node in a Linked List --- 删除链表中结点

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 ->

[CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.EXAMPLEInput: the node c from the linked list a->b->c->d->eResult: nothing is returned, but the new linked list looks like a- >

图解精选 TOP 面试题 001 | LeetCode 237. 删除链表中的节点

题目描述 原题链接 LeetCode 237. 删除链表中的节点:https://leetcode-cn.com/problems/delete-node-in-a-linked-list 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 head =?[4,5,1,9],它可以表示为: 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 ?5? 的第二个节点,那么在调

[LeetCode]79. Delete Node in a Linked List删除链表节点

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 ->

[LeetCode] Delete Node in a Linked List 删除链表的节点

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 ->

lintcode 容易题:Remove Nth Node From End of Lis 删除链表中倒数第n个节点

题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点.  样例 给出链表1->2->3->4->5->null和 n = 2. 删除倒数第二个节点之后,这个链表将变成1->2->3->5->null. 注意 链表中的节点个数大于等于n 解题: 要删除倒数第n个节点,我们要找到其前面一个节点,也就是倒数第n+1的节点,找到这个节点就可以进行删除.和上题的思想很类似, 定义两个指针,p和cur,cur指针向前走,走了n

删除链表中的节点

1.删除链表中等于给定值 val 的所有节点. class Solution { public: ListNode* removeElements(ListNode* head, int val) { ListNode*newHead=new ListNode(-1); ListNode*pre=newHead; pre->next=head; while(pre->next!=NULL){ ListNode*cur=pre->next; if(cur->val==val){ pr

【leetcode 简单】 第六十九题 删除链表中的节点

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 4 -> 5 -> 1 -> 9 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 示例 2: 输入: head = [4,5,1,9], node = 1 输

LeetCode237 删除链表中的节点

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 4 -> 5 -> 1 -> 9 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 示例 2: 输入: head = [4,5,1,9], node = 1 输