Skip to content

Instantly share code, notes, and snippets.

@RaminMammadzada
Created December 3, 2021 08:13
Show Gist options
  • Save RaminMammadzada/fa83ae30ca348fadbd1bf5f47f2495ac to your computer and use it in GitHub Desktop.
Save RaminMammadzada/fa83ae30ca348fadbd1bf5f47f2495ac to your computer and use it in GitHub Desktop.
CodeSignal-removeKFromList
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
solution(l, k) = [1, 2, 4, 5];
For l = [1, 2, 3, 4, 5, 6, 7] and k = 10, the output should be
solution(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.
[Python 3] Syntax Tips
# Prints help message to the console
# Returns a string
def helloWorld(name):
print("This prints to the console when you Run Tests")
return "Hello, " + name
@RaminMammadzada
Copy link
Author

# Singly-linked lists are already defined with this interface:
# class ListNode(object):
#   def __init__(self, x):
#     self.value = x
#     self.next = None
#
def solution(l, k):
    if l is None:
        return l
    
    while l.value == k and l != None:
        l = l.next
        if l == None:
            break
        
    if l is None:
        return l
    
    curr = l
    while curr.next:
        if int(curr.next.value == k):
            curr.next = curr.next.next
        else:
            curr = curr.next
        
    return l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment