1

I am trying to call/print the following linked list:

#--the following code is taken from the most upvoted solution at codesignal--
 class ListNode(object):
   def __init__(self, x):
     self.value = x
     self.next = None

def removeKFromList(l, k):
    c = l
    while c:
        if c.next and c.next.value == k:
            c.next = c.next.next
        else:
            c = c.next
    return l.next if l and l.value == k else l

I want to call/instantiate the class with l = [3, 1, 2, 3, 4, 5] and k = 3 and I am expecting it to return => [1, 2, 4, 5] Basically, I am not able to first instantiate the ListNode class and then call the removeKFromList method inside it.. You can find this challenge at: https://app.codesignal.com/interview-practice/task/gX7NXPBrYThXZuanm/description

Description:

Note: Try to solve this task in O(n) time using O(1) additional space, where n is the number of elements in the list, since this is what you'll be asked to do during an interview.

Given a singly linked list of integers l and an integer k, remove all elements from list l that have a value equal to k.

Example

For l = [3, 1, 2, 3, 4, 5] and k = 3, the output should be

removeKFromList(l, k) = [1, 2, 4, 5]

For l = [1, 2, 3, 4, 5, 6, 7] and k = 10, the output should be

removeKFromList(l, k) = [1, 2, 3, 4, 5, 6, 7]

Input/Output

execution time limit: 4 seconds (py3)

input: linkedlist.integer l

A singly linked list of integers.

Guaranteed constraints:

  • 0 ≤ list size ≤ 105,
  • -1000 ≤ element value ≤ 1000.

input: integer k

An integer.

Guaranteed constraints:

  • -1000 ≤ k ≤ 1000.

output: linkedlist.integer

Return l with all the values equal to k removed.

tseeker
  • 79
  • 9
  • What does "I am not able to" mean? What have you tried? – Dennis Jun 23 '21 at 04:36
  • Literally, I want to TEST it with some INPUT and I want to get some OUTPUT – tseeker Jun 23 '21 at 04:37
  • What function do you think should be taking a Python list and turning it into a linked list of your `ListNode`s? (Hint: you have not made any attempt at writing such a function yet.) – Samwise Jun 23 '21 at 04:38
  • Does this answer your question? [Python Linked List](https://stackoverflow.com/questions/280243/python-linked-list) – Samwise Jun 23 '21 at 04:41
  • No, could you please give me the code for your suggested solution? – tseeker Jun 23 '21 at 04:42
  • On the codesignal(that I have shared) site where I found this challenge, it says "Singly-linked lists are already defined with this interface:"; however, when I see solutions, I can't test them with actual test inputs on my side.. – tseeker Jun 23 '21 at 04:49
  • I have tried something like this ```a = ListNode({}) print(a.removeKFromList([1,2,3], 3))``` but not working.. – tseeker Jun 23 '21 at 04:50
  • 1
    This is a challenge. It's cheating to ask us to give you the solution. – Tim Roberts Jun 23 '21 at 05:21
  • I think you may be getting confused by the name `list` that Python also uses for its own type. Python's lists are not the same as the linked lists this code is using. They don't interoperate in any way unless *you* write code to make them do so. `l = [3, 1, 2, 3, 4, 5]` creates a Python `list`, not any instances of `ListNode`. – Blckknght Jun 23 '21 at 05:25
  • @TimRoberts, I am trying to give as detailed explanation of this challenge as I can, including what I have tried and what I would like to see.. and you're commenting basically without any logical suggestion.. I am wondering what was your intention to post then... and actually, it is not about SOLUTION as I am actually using the SOLUTION to understand the code!!! – tseeker Jun 23 '21 at 05:28
  • @Blckknght, so what I am trying to do, which MIGHT be wrong, that is why -- I am using this platform, is -- first I am trying to instantiate the ListNode class, like this ```a = ListNode``` then, logically, I am trying to access that instantiated class 's ```removeKFromList``` method with some provided parameters for ```l, k``` but I am getting this: ```AttributeError: 'list' object has no attribute 'next'``` – tseeker Jun 23 '21 at 05:43
  • @Blckknght, also, I noticed from the solution, that the ```removeKFromList``` function/method is located outside of the ```ListNode``` class, and I know that Python is very strict on indentation.. – tseeker Jun 23 '21 at 05:47
  • The link to code signal is useless for people that don't have an account there. The question should have all information needed to understand it, not behind a link. Can you add it (formatted as a quote)? – trincot Jun 23 '21 at 05:54
  • @trincot, have added to my post – tseeker Jun 23 '21 at 06:05
  • You should also add the attempts (which you described in comments above) inside your question. It will help people understand what the problem is you are having. – trincot Jun 23 '21 at 06:32

1 Answers1

3

Looking at the comments you made, there are some misunderstandings to resolve here:

How to create a list

It seems you expected one of these to create a list:

a = ListNode({}) 

or

a = ListNode

Neither is what you need. The ListNode constructor will create one node of a potentially linked list, but you must establish the links "yourself". Passing {} to that constructor makes little sense: that {} is a dictionary and has nothing to do with this data structure. The second form will not even call the constructor, it merely copies the class object to your variable a, which is not what you want.

Putting it in very verbose terms, the example list is created like this:

l = ListNode(3)
l.next = ListNode(1)
l.next.next = ListNode(2)
l.next.next.next = ListNode(3)
l.next.next.next.next = ListNode(4)
l.next.next.next.next.next = ListNode(5)

This should highlight how a list is created, but of course it does not look very practical. See further down for a better way.

How to call the function

You mentioned in comments the following call:

a.removeKFromList([1,2,3], 3)

But there are two issues with that:

  1. It assumes that removeKFromList is a method of ListNode (since you prefix it with a.), but it should be a plain function, not a method

  2. The first argument is a standard list, but the code challenge tells you it expects a linked list as first argument

You would need to call your function as follows:

result = removeKFromList(l, 3)

Utility functions

To test your code, add the following functions, which will help you instantiate a linked list (from a standard list) and to verify the content of a linked list (through iterating its values):

def createList(lst):
    head = None
    for val in reversed(lst):
        node = ListNode(val)
        node.next = head
        head = node
    return head

def iterList(head):
    while head:
        yield head.value
        head = head.next

These will ease your tests.

So now your driver code -- for turning an input to an output -- can be:

# define the input
l = createList([3, 1, 2, 3, 4, 5])
k = 3
# run your algorithm
result = removeKFromList(l, 3)
# verify it
print(*iterList(result))

This outputs the expected output for the given example.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    Thanks a lot for for your detailed answer, really appreciate it! Now I am trying to learn your provided code with step by step execution/info at http://pythontutor.com/ :) – tseeker Jun 23 '21 at 17:38