Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Python/linkedlist/Remove_dublicates_from_sortedList.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]:
if not head:
return None

slow = head
fast = head.next
preslow = head
while fast:

while slow.val == fast.val:
fast = fast.next

preslow.next = fast
slow = slow.next

if fast:
fast = fast.next
return head
26 changes: 26 additions & 0 deletions Python/linkedlist/addTwoNumbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(
self, l1: Optional[ListNode], l2: Optional[ListNode]
) -> Optional[ListNode]:
dummy = ListNode()
res = dummy
total = carry = 0
while l1 or l2 or carry:

total = carry
if l1:
total += l1.val
l1 = l1.next
if l2:
total += l2.val
l2 = l2.next
num = total % 10
carry = total // 10
dummy.next = ListNode(num)
dummy = dummy.next
return res.next
22 changes: 22 additions & 0 deletions Python/linkedlist/mergeTwoSortedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def mergeTwoLists(
self, list1: Optional[ListNode], list2: Optional[ListNode]
) -> Optional[ListNode]:
dummy = ListNode(0)
cur = dummy
while list1 and list2:
if list1.val >= list2.val:
cur.next = list2
list2 = list2.next
else:
cur.next = list1
list1 = list1.next
cur = cur.next
if list1:
cur.next = list1

else:
cur.next = list2
return dummy.next


23 changes: 23 additions & 0 deletions Python/linkedlist/removeNthSortedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
cur = head
length = 1
while cur.next:
cur = cur.next
length += 1
position = length - n - 1
if n == length:
return head.next

now = head
for _ in range(position):
now = now.next

now.next = now.next.next

return head
23 changes: 23 additions & 0 deletions Python/linkedlist/rotateList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:

dummy = head
length = 1
while dummy.next:
dummy = dummy.next
lenght += 1
position = k % length

current = head
for _ in range(length - position - 1):
current = current.next
new_head = current.next
current_next = None
dummy.next = head

return new_head
22 changes: 22 additions & 0 deletions Python/linkedlist/swapNodesinPair.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0, head)
cur = dummy

while head and head.next:
first = head
second = head.next

cur.next = second
first.next = second.next
second.next = first

dummy = first
head = first.next

return dummy.next