vJechsmayr / PythonAlgorithms

All Algorithms implemented in Python 3 a project for hacktoberfest2020 - NO Issues or PRs for hacktoberfest 2021
MIT License
132 stars 367 forks source link

0203 - Remove Linked List Elements #743

Closed javmarina closed 4 years ago

javmarina commented 4 years ago

Description of the Problem

Remove all elements from a linked list of integers that have value val.

Example:

Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5

Code

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        while head:
            if head.val == val:
                head = head.next
            else:
                break
        currentNode = head
        while currentNode:
            while currentNode.next and currentNode.next.val == val:
                currentNode.next = currentNode.next.next

            currentNode = currentNode.next
        return head

Link To The LeetCode Problem

LeetCode

flick-23 commented 4 years ago

Please assign this to me. I will do it!

javmarina commented 4 years ago

I already proposed a solution