-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete-node-in-a-linked-list.js
52 lines (50 loc) · 1.16 KB
/
delete-node-in-a-linked-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Source: https://leetcode.com/problems/delete-node-in-a-linked-list/
* Tags: [Linked List]
* Level: Easy
* Title: Delete Node in a Linked List
* Auther: @imcoddy
* Content: 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.
*
*
*
*
*
*
*
*
*
* Show Similar Problems
*
*
* (E) Remove Linked List Elements
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} node
* @return {void} Do not return anything, modify node in-place instead.
*/
/**
* Memo: Since the node won't be the last one, simply copy its next node's value and link it to its next as well
* Complex: O(1)
* Runtime: 148ms
* Tests: 83 test cases passed
* Rank: S
* Updated: 2015--
*/
var deleteNode = function(node) {
if (node.next) {
node.val = node.next.val;
node.next = node.next.next;
}
};