【Leetcode】Delete Node in a Linked List

原题链接:https://leetcode.com/problems/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 -> 4 after calling your function.

思路:

删除链表结点有两种思路,一、改变指针指向,不改变结点值 ,二、改变结点值

条件只给了要删除结点p的指针,没有给出头结点,所以不能得到p的前结点pre,也就无法简单的让pre指向p后面的结点完成删除p的操作。

所以只能通过结点值的操作达到删除的目的。注意,这种方法不能删除最后一个结点(能删头结点)。幸运的是,题目只要求删除中间结点。

算法:

[java] view
plain
 copy

  1. public void deleteNode(ListNode node) {
  2. ListNode pre = node, p = node;
  3. while (p.next != null) {// 当p不是最后一个结点时
  4. p.val = p.next.val;
  5. pre = p;
  6. p = p.next;
  7. }
  8. pre.next = null;// 此时pre指向倒数第二个结点
  9. }
时间: 2024-08-14 03:55:40

【Leetcode】Delete Node in a Linked List的相关文章

[LeetCode][JavaScript]Delete Node in a Linked List

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

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

leetcode:237 Delete Node in a Linked List-每日编程第四题

Delete Node in a Linked List Total Accepted: 47385 Total Submissions: 107608 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 ->

【LeetCode OJ 237】Delete Node in a Linked List

题目链接:https://leetcode.com/problems/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 th

【LeetCode 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 list should become 1 -> 2 ->

【LeetCode】Flatten Binary Tree to Linked List 解题报告

[题目] Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 [解析] 题意是把一棵二叉树按照先序遍历的方式放到一棵只有右支树的二叉树中. 最开始想的思路是递归发,后来发现这样会溢出. 然后就用一个栈和一个队列来实现,队列用来存储先序遍历的结果,栈用于先序遍历.

LeetCode (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 list should become 1 -> 2 ->

[leetcode] 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 list should become 1 -> 2 ->

[LeetCode] 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 list should become 1 -> 2 ->