반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 237 | Delete Node in a Linked List | Python 본문
728x90
반응형
문제 링크: https://leetcode.com/problems/delete-node-in-a-linked-list/
문제
There is a singly-linked list head and we want to delete a node node in it.
You are given the node to be deleted node. You will not be given access to the first node of head.
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.
Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:
- The value of the given node should not exist in the linked list.
- The number of nodes in the linked list should decrease by one.
- All the values before node should be in the same order.
- All the values after node should be in the same order.
Custom testing:
- 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.
- We will build the linked list and pass the node to your function.
- The output will be the entire list after calling your function.
조건
- The number of the nodes in the given list is in the range [2, 1000].
- -1000 <= Node.val <= 1000
- The value of each node in the list is unique.
- The node to be deleted is in the list and is not a tail node.
내 풀이
아래 그림과 같이 진행한다.
없애야 하는 노드가 node의 맨 앞부분이다.
리턴을 하면 안되므로, node의 val을 node.next의 val과 같게 만든다.
그리고 node.next를 node.next.next로 바꾼다.
코드
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val = node.next.val
node.next = node.next.next
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 38 | Count and Say | Python (0) | 2022.10.18 |
---|---|
[2095] Delete the Middle Node of a Linked List | Python (0) | 2022.10.14 |
[LeetCode] 976 | Largest Perimeter Triangle | Python (0) | 2022.10.12 |
[LeetCode] 334 | Increasing Triplet Subsequence | Python (0) | 2022.10.11 |
[LeetCode] 981 | Time Based Key-Value Store | Python (0) | 2022.10.06 |
Comments