237.单链表移除节点 Delete Node in a Linked List

Question

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 -> 4after calling your function.

Solution

Approach: Swap with Next Node [Accepted]

The usual way of deleting a node node from a linked list is to modify the next pointer of the node before it, to point to the node after it.

Since we do not have access to the node before the one we want to delete, we cannot modify the next pointer of that node in any way. Instead, we have to replace the value of the node we want to delete with the value in the node after it, and then delete the node after it.

Because we know that the node we want to delete is not the tail of the list, we can guarantee that this approach is possible.

  1. public void DeleteNode(ListNode node) {
  2. node.val = node.next.val;
  3. node.next = node.next.next;
  4. }

来自为知笔记(Wiz)

时间: 2024-11-15 13:51:00

237.单链表移除节点 Delete Node in a Linked List的相关文章

leetCode 237. Delete Node in a Linked List 链表

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

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

Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]

题目: 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 -

图解精选 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? 的第二个节点,那么在调

快慢指针原理--快速找到未知长度单链表的中间节点

package com.java.dataStruct; //节点类 public class Node<E> { E item; Node next; public Node(){ } public Node(E element){ this.item = element; } public Node(E element, Node next){ this.item = element; this.next = next; } } Node p1,r1; Node L1 = new Node

求单链表的中间节点,用快慢指针

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Node* findMid(Node* &head) {     if(head == NULL||head->next == NULL)         return head;          Node* p = head;     Node* q = head;     while(q->next->next&&(q = q->next))     {         p = p-

单链表判断公共节点

单链表判断有无公共节点是个比较有趣的问题.这里所说的公共节点指的是完全相同的节点,不同与一般意义上的节点元素相同.相交单链表简单的都会是如下形式(有环除外): 粗略分析,容易想到就是暴力法,双重循环寻找公共节点. 关于单链表的判断有无公共节点,除了暴力法之外,还有很多方法可以尝试.下面简单列举几种. 可以尝试hash,如果两个节点的首地址相同,则该节点必定相同,可以以空间换取时间,先处理其中一个单链表,建立hash表,然后处理另外一个判断有无公共节点,时间复杂度为O(max(n,m)),空间复杂

从无头单链表中删除节点

1.从无头单链表中删除节点 一个没有头指针的单链表.一个指针指向此单链表中间的一个节点(不是第一个也不是最后一个节点).将该节点删除. A-->B-->C-->D       =====>     A-->C-->D 问题解析:由于只能单向遍历,故与删除B节点,无法得知B的前驱A,即无法像普通删除中那样让A的next指向C; 这里有个技巧,将C当做B,而B复制成C,则删除C节点即可: 2.给定一个链表的头指针,要求只遍历一次,将单链表中的元素顺序反转过来. A-->

C语言:【单链表】查找单链表的中间节点,要求只能遍历一次

#include<stdio.h> #include<assert.h> #include<stdlib.h> typedef int DataType; typedef struct SListNode {     DataType data;     struct SListNode* next; }SListNode; SListNode* BuyNode(DataType x) {     SListNode* next = (SListNode*)malloc