# Leetcode: 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 should become 1 -> 2 -> 4 after calling your function.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

```## Blog link: https://code.dennyzhang.com/delete-node-in-a-linked-list
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
## Idea: update the value p.val with p.next.val, then remove p.next
## Complexity: Time O(n), Space O(1)
## Data Sample
##    1 -> 2 -> 3 -> 4 -> 5
##              node p
p = node.next # q will always exist
node.val = p.val
node.next = p.next
```

Share It, If You Like It.