Skip to content

Commit

Permalink
Solved new problems
Browse files Browse the repository at this point in the history
  • Loading branch information
ghanteyyy committed Mar 1, 2023
1 parent b30ed89 commit 6b0c18a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
48 changes: 48 additions & 0 deletions LeetCode/21. Merge Two Sorted Lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def UpdateNode(self, val):
temp = self.new_head
newNode = ListNode(val)

if temp == None:
self.new_head = self.lastNode = newNode

else:
self.lastNode.next = newNode
self.lastNode = newNode

def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
self.new_head = self.lastNode = None

while list1 and list2:
val1 = list1.val
val2 = list2.val

if val1 == val2:
self.UpdateNode(val1)
self.UpdateNode(val1)

list1 = list1.next
list2 = list2.next

elif val1 < val2:
self.UpdateNode(val1)
list1 = list1.next

else:
self.UpdateNode(val2)
list2 = list2.next

while list1:
self.UpdateNode(list1.val)
list1 = list1.next

while list2:
self.UpdateNode(list2.val)
list2 = list2.next

return self.new_head
24 changes: 24 additions & 0 deletions LeetCode/83. Remove Duplicates from Sorted List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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: Optional[ListNode]) -> Optional[ListNode]:
prev = new_head = last_node = None

while head is not None:
if head.val != prev:
value = prev = head.val
new_node = ListNode(value)

if new_head is None:
new_head = last_node = new_node

else:
last_node.next = new_node
last_node = new_node

head = head.next

return new_head

0 comments on commit 6b0c18a

Please sign in to comment.