diff --git a/README.md b/README.md deleted file mode 100644 index 7c3b229..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Algorithms 2019-2020 Python LEETCODE - -Check branches for information. \ No newline at end of file diff --git a/linked-list.md b/linked-list.md new file mode 100644 index 0000000..cb08825 --- /dev/null +++ b/linked-list.md @@ -0,0 +1,53 @@ +# Linked list + ++ [Merge Two Sorted Lists (recursive)](#merge-two-sorted-lists-recursive) ++ [Merge Two Sorted Lists (iterative)](#merge-two-sorted-lists-iterative) + +## Merge Two Sorted Lists (recursive) + +https://leetcode.com/problems/merge-two-sorted-lists/ + +```python +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution(object): + def mergeTwoLists(self, a, b): + if not a or b and a.val > b.val: + a, b = b, a + if a: + a.next = self.mergeTwoLists(a.next, b) + return a + +``` + +## Merge Two Sorted Lists (iterative) + +https://leetcode.com/problems/merge-two-sorted-lists/ + +```python +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution(object): + def mergeTwoLists(self, a, b): + head = tail = ListNode() + while a and b: + if a.val < b.val: + tail.next = a + a = a.next + else: + tail.next = b + b = b.next + tail = tail.next + if a: + tail.next = a + else: + tail.next = b + return head.next + +``` \ No newline at end of file