It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.
Apparently, many engineers take measures on the site called LetCode.
It is a site that trains the algorithmic power that can withstand the coding test that is done in the early story, and it is an inevitable path for those who want to build a career at an overseas tech company.
I wrote it in a big way, but I have no plans to have such an interview at the moment.
However, as an IT engineer, it would be better to have the same level of algorithm power as a person, so I would like to solve the problem irregularly and write down the method I thought at that time as a memo.
I'm solving it with Python3.
Leet Code Table of Contents Starting from Zero
Last time Leet Code Day 61 "7. Reverse Integer" starting from zero
Right now, I'm prioritizing the Medium of the Top 100 Liked Questions. I solved all Easy, so if you are interested, please go to the table of contents.
Twitter I'm doing it.
** Technical Blog Started! !! ** ** I think the technology will write about LetCode, Django, Nuxt, and so on. ** This is faster to update **, so please bookmark it!
83. Remove Duplicates from Sorted List
The difficulty level is Easy.
I didn't manage the problem I solved and the problem I wrote the article well, so I thought it was written clearly.
-Leet Code Day48 starting from zero "26. Remove Duplicates from Sorted Array"
Only this one was writing. The name was confusing and I laughed unintentionally.
Now, the problem this time is that we are given a linked list, so remove the Duplicates so that all the elements appear only once.
Example 1:
Input: 1->1->2 Output: 1->2 Example 2:
Input: 1->1->2->3->3 Output: 1->2->3
I wrote as follows. Is it peculiar to other languages? There are few elements that become, and I think that people who are writing in other languages can also read it.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
runner = head
while runner:
while runner.next and runner.next.val == runner.val:
runner.next = runner.next.next
runner = runner.next
return head
# Runtime: 48 ms, faster than 31.67% of Python3 online submissions for Remove Duplicates from Sorted List.
# Memory Usage: 14 MB, less than 21.02% of Python3 online submissions for Remove Duplicates from Sorted List.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
origin = head
pre = head
head = None if head is None else head.next
while head:
if pre.val == head.val:
pre.next = head.next
else:
pre = pre.next
head = head.next
return origin
# Runtime: 44 ms, faster than 58.51% of Python3 online submissions for Remove Duplicates from Sorted List.
# Memory Usage: 13.8 MB, less than 55.82% of Python3 online submissions for Remove Duplicates from Sorted List.
On the other hand, it was faster to write by holding two.
If you force it, you may be confused for a moment because the first assignment to head
is written in the inclusion notation.
In short
if head is None:
head = None
else:
head.next
I just wrote it briefly. I personally like it because it can be combined into one when doing a return. It's good to be short and enjoyable to write.
When I saw the origin, I wanted to eat the origin lunch, so this time it is up to here. Thank you for your hard work.
Recommended Posts