Skip to content

Commit eff82c1

Browse files
Create 237_Delete_node_in_linked_list.py
237. Delete Node in a Linked List solution implemented.
1 parent fac7710 commit eff82c1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

237_Delete_node_in_linked_list.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
237. Delete Node in a Linked List
3+
There is a singly-linked list head and we want to delete a node node in it.
4+
5+
You are given the node to be deleted node. You will not be given access to the first node of head.
6+
7+
All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list.
8+
9+
Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:
10+
11+
The value of the given node should not exist in the linked list.
12+
The number of nodes in the linked list should decrease by one.
13+
All the values before node should be in the same order.
14+
All the values after node should be in the same order.
15+
Custom testing:
16+
17+
For the input, you should provide the entire linked list head and the node to be given node. node should not be the last node of the list and should be an actual node in the list.
18+
We will build the linked list and pass the node to your function.
19+
The output will be the entire list after calling your function.
20+
''''
21+
22+
# Definition for singly-linked list.
23+
# class ListNode:
24+
# def __init__(self, x):
25+
# self.val = x
26+
# self.next = None
27+
28+
class Solution:
29+
def deleteNode(self, node):
30+
"""
31+
:type node: ListNode
32+
:rtype: void Do not return anything, modify node in-place instead.
33+
"""
34+
temp = node.next
35+
node.val = temp.val
36+
node.next = node.next.next
37+

0 commit comments

Comments
 (0)