Pythonのコードで以下のようなものを見ました。
LeetCodeでの解答のひとつです。
Python
1# Definition for singly-linked list. 2# class ListNode: 3# def __init__(self, x): 4# self.val = x 5# self.next = None 6 7class Solution: 8 def deleteNode(self, node): 9# :type node: ListNode 10# :rtype: void Do not return anything, modify node in-place instead. 11 pre = node 12 p = node.next 13 while p.next != None: 14 pre.val = p.val 15 pre = p 16 p = p.next 17 pre.val = p.val 18 pre.next = None
イテレータの要素を削除するような問題だと思いますが、コード内の例えば、
node.next や
pre.val の
nextとvalが何なのかよくわかりません。
メソッドなのだとは思いますが、本で調べてもネットで検索しても、
next()メソッドがnext(pre)で利用されているサンプルはありますが、上記の書き方で利用されている例がみつからないのです。
valも同様で、pre.valという書き方もメソッドもみつかりません。
これらは、どういうものなのでしょうか。
Leetcodeではみなさん当然のように書いているのですが理解が進まなくて質問しました。
Leetcodeでの問題は以下の通りです。
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Given linked list -- head = [4,5,1,9], which looks like following:
4 → 5 → 1 → 9
Example 1:
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/24 16:05
2019/07/24 16:08
2019/07/25 07:53
2019/07/25 08:01
2019/07/25 08:24