diff --git a/Python/linkedlist/Remove_dublicates_from_sortedList.py b/Python/linkedlist/Remove_dublicates_from_sortedList.py new file mode 100644 index 0000000..00313ae --- /dev/null +++ b/Python/linkedlist/Remove_dublicates_from_sortedList.py @@ -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 diff --git a/Python/linkedlist/addTwoNumbers.py b/Python/linkedlist/addTwoNumbers.py new file mode 100644 index 0000000..6e52472 --- /dev/null +++ b/Python/linkedlist/addTwoNumbers.py @@ -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 diff --git a/Python/linkedlist/mergeTwoSortedList.py b/Python/linkedlist/mergeTwoSortedList.py new file mode 100644 index 0000000..09a7b5f --- /dev/null +++ b/Python/linkedlist/mergeTwoSortedList.py @@ -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 + + diff --git a/Python/linkedlist/removeNthSortedList.py b/Python/linkedlist/removeNthSortedList.py new file mode 100644 index 0000000..1cc7193 --- /dev/null +++ b/Python/linkedlist/removeNthSortedList.py @@ -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 diff --git a/Python/linkedlist/rotateList.py b/Python/linkedlist/rotateList.py new file mode 100644 index 0000000..e68767c --- /dev/null +++ b/Python/linkedlist/rotateList.py @@ -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 diff --git a/Python/linkedlist/swapNodesinPair.py b/Python/linkedlist/swapNodesinPair.py new file mode 100644 index 0000000..9941ba5 --- /dev/null +++ b/Python/linkedlist/swapNodesinPair.py @@ -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