This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, data): | |
self.data = data | |
self.next = None | |
class LinkedList: | |
def __init__(self): | |
self.head = None | |
self.tail = None | |
self.length = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
class Node: | |
def __init__(self, data): | |
self.data = data | |
self.next = None | |
class LinkedList: | |
def __init__(self): | |
self.head = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Версия "в лоб" - O(n^2) | |
def two_sum_brute(nums, target): | |
for i in range(len(nums)): | |
for j in range(i + 1, len(nums)): | |
if nums[i] + nums[j] == target: | |
return [i, j] | |
return [] | |
# Улучшенная версия с хэш-таблицей - O(n) | |
def two_sum_optimized(nums, target): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def remove_duplicates(nums): | |
if not nums: | |
return 0 | |
i = 0 | |
for j in range(1, len(nums)): | |
if nums[j] != nums[i]: | |
i += 1 | |
nums[i] = nums[j] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListNode: | |
def __init__(self, val=0, next=None): | |
self.val = val | |
self.next = next | |
def reverse_list(head): | |
prev = None | |
current = head |
OlderNewer