forked from ghanteyyy/nppy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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 |