DeleteNodeInALinkedList [source code]
public class DeleteNodeInALinkedList {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
这个题目更像是一个脑精急转弯. 常规想法, 如果是 delete 一个 node, 那么做法肯定要有这个node 的 parent 的 access, 因为parent.next肯定也要修改; 但是这一题的难点就在这里, 你并没有node.parent;
最后实际的做法是比较讨巧的, 严格意义上来说, 最后被 delete 的其实还node.next而不是 node 自己, 但是最后得到的结果是一样的. 实际面试的时候要是碰到这种问题也是认倒霉了.
Problem Description
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.
Difficulty:Easy
Category:Algorithms
Acceptance:46.20%
Contributor: LeetCode
Companies
adobe apple microsoft
Related Topics
linked list
Similar Questions
Remove Linked List Elements