From 42fb596ac01416f4a32db850222606613979a5fd Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Wed, 13 May 2020 20:53:05 +0300 Subject: [PATCH 01/12] +linked-list.md +reverse-linked-list --- README.md | 3 --- linked-list.md | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) delete mode 100644 README.md create mode 100644 linked-list.md 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..ea69dbe --- /dev/null +++ b/linked-list.md @@ -0,0 +1,35 @@ +# Linked list + +## Reverse linked list + +https://leetcode.com/problems/reverse-linked-list/ + +```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 reverseList(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + + if head and head.next: + prev = head + next = head.next + head.next = None + head = next + + while (head.next): + next = next.next + head.next = prev + prev = head + head = next + + head.next = prev + return head + +``` \ No newline at end of file From 080e83306502ec737c9e60ef6cdf5c9819fb529d Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Fri, 22 May 2020 19:15:05 +0300 Subject: [PATCH 02/12] Changed variables' name, added hyperlinks (anchors) --- linked-list.md | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/linked-list.md b/linked-list.md index ea69dbe..a3ea47e 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,5 +1,7 @@ # Linked list ++ [Reverse linked list](#reverse-linked-list) + ## Reverse linked list https://leetcode.com/problems/reverse-linked-list/ @@ -16,20 +18,20 @@ class Solution(object): :type head: ListNode :rtype: ListNode """ + currentNode = head + if currentNode and currentNode.next: + prevNode = currentNode + nextNode = currentNode.next + currentNode.next = None + currentNode = nextNode - if head and head.next: - prev = head - next = head.next - head.next = None - head = next - - while (head.next): - next = next.next - head.next = prev - prev = head - head = next + while currentNode.next: + nextNode = nextNode.next + currentNode.next = prevNode + prevNode = currentNode + currentNode = nextNode - head.next = prev - return head + currentNode.next = prevNode + return currentNode ``` \ No newline at end of file From 0d5a6623490c347ee16f1dc29556a76c7f059ccc Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Fri, 22 May 2020 19:22:14 +0300 Subject: [PATCH 03/12] Improved [Reverse linked list] --- linked-list.md | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/linked-list.md b/linked-list.md index a3ea47e..cb55b12 100644 --- a/linked-list.md +++ b/linked-list.md @@ -19,19 +19,14 @@ class Solution(object): :rtype: ListNode """ currentNode = head - if currentNode and currentNode.next: - prevNode = currentNode + prevNode = None + + while currentNode: nextNode = currentNode.next - currentNode.next = None + currentNode.next = prevNode + prevNode = currentNode currentNode = nextNode - while currentNode.next: - nextNode = nextNode.next - currentNode.next = prevNode - prevNode = currentNode - currentNode = nextNode - - currentNode.next = prevNode - return currentNode + return prevNode ``` \ No newline at end of file From 57b19108930dfa4937849b8f266de07d5969ff99 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Mon, 1 Jun 2020 21:35:22 +0300 Subject: [PATCH 04/12] Added LEETCODE middle-of-the-linked-list solution. --- linked-list.md | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/linked-list.md b/linked-list.md index cb55b12..f0ef4c0 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,10 +1,10 @@ # Linked list -+ [Reverse linked list](#reverse-linked-list) ++ [Middle of the Linked List](#middle-of-the-linked-list) -## Reverse linked list +## Middle of the Linked List -https://leetcode.com/problems/reverse-linked-list/ +https://leetcode.com/problems/middle-of-the-linked-list/ ```python # Definition for singly-linked list. @@ -13,20 +13,15 @@ https://leetcode.com/problems/reverse-linked-list/ # self.val = val # self.next = next class Solution(object): - def reverseList(self, head): + def middleNode(self, head): """ :type head: ListNode :rtype: ListNode """ - currentNode = head - prevNode = None - - while currentNode: - nextNode = currentNode.next - currentNode.next = prevNode - prevNode = currentNode - currentNode = nextNode - - return prevNode + pastNode = nextNode = head + while nextNode and nextNode.next: + pastNode = pastNode.next + nextNode = nextNode.next.next + return pastNode ``` \ No newline at end of file From 303c7ae0c16ae0782ce00ec5838a2ac70e422cd4 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Mon, 1 Jun 2020 22:31:00 +0300 Subject: [PATCH 05/12] Added LEETCODE palindrome-linked-list solution. --- linked-list.md | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/linked-list.md b/linked-list.md index f0ef4c0..df298f4 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,10 +1,10 @@ # Linked list -+ [Middle of the Linked List](#middle-of-the-linked-list) ++ [Palindrome Linked List](#palindrome-linked-list) -## Middle of the Linked List +## Palindrome Linked List -https://leetcode.com/problems/middle-of-the-linked-list/ +https://leetcode.com/problems/palindrome-linked-list/ ```python # Definition for singly-linked list. @@ -13,15 +13,27 @@ https://leetcode.com/problems/middle-of-the-linked-list/ # self.val = val # self.next = next class Solution(object): - def middleNode(self, head): + def isPalindrome(self, head): """ :type head: ListNode - :rtype: ListNode + :rtype: bool """ - pastNode = nextNode = head - while nextNode and nextNode.next: - pastNode = pastNode.next - nextNode = nextNode.next.next - return pastNode + mid = tail = head + while tail and tail.next: + mid = mid.next + tail = tail.next.next + + if mid is None: + return True + if mid.next is None: + return head.val == mid.val + + mid.next.next, mid.next, tail, mid = mid, None, mid.next.next, mid.next + while tail: + tail.next, mid, tail = mid, tail, tail.next + + while mid and head.val == mid.val: + head, mid = head.next, mid.next + return not mid ``` \ No newline at end of file From cbc585fcdc3ed88c070c912ac4b2ca6cee39418f Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Mon, 1 Jun 2020 22:48:34 +0300 Subject: [PATCH 06/12] Added LEETCODE merge-two-sorted-lists solution. --- linked-list.md | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/linked-list.md b/linked-list.md index df298f4..f350957 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,10 +1,10 @@ # Linked list -+ [Palindrome Linked List](#palindrome-linked-list) ++ [Merge Two Sorted Lists](#merge-two-sorted-lists) -## Palindrome Linked List +## Merge Two Sorted Lists -https://leetcode.com/problems/palindrome-linked-list/ +https://leetcode.com/problems/merge-two-sorted-lists/ ```python # Definition for singly-linked list. @@ -13,27 +13,11 @@ https://leetcode.com/problems/palindrome-linked-list/ # self.val = val # self.next = next class Solution(object): - def isPalindrome(self, head): - """ - :type head: ListNode - :rtype: bool - """ - mid = tail = head - while tail and tail.next: - mid = mid.next - tail = tail.next.next - - if mid is None: - return True - if mid.next is None: - return head.val == mid.val - - mid.next.next, mid.next, tail, mid = mid, None, mid.next.next, mid.next - while tail: - tail.next, mid, tail = mid, tail, tail.next - - while mid and head.val == mid.val: - head, mid = head.next, mid.next - return not mid + 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 ``` \ No newline at end of file From 70ddb4f72808c02114117d382353a393669a3de9 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Mon, 1 Jun 2020 23:05:26 +0300 Subject: [PATCH 07/12] Added LEETCODE remove-nth-node-from-end-of-list solution. --- linked-list.md | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/linked-list.md b/linked-list.md index f350957..8902728 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,10 +1,10 @@ # Linked list -+ [Merge Two Sorted Lists](#merge-two-sorted-lists) ++ [Remove Nth Node From End of List](#remove-nth-node-from-end-of-list) -## Merge Two Sorted Lists +## Remove Nth Node From End of List -https://leetcode.com/problems/merge-two-sorted-lists/ +https://leetcode.com/problems/remove-nth-node-from-end-of-list/ ```python # Definition for singly-linked list. @@ -12,12 +12,17 @@ https://leetcode.com/problems/merge-two-sorted-lists/ # 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 +class Solution: + def removeNthFromEnd(self, head, n): + pastNode = nextNode = head + for i in range(n): + nextNode = nextNode.next + if not nextNode: + return head.next + while nextNode.next: + nextNode = nextNode.next + pastNode = pastNode.next + pastNode.next = pastNode.next.next + return head ``` \ No newline at end of file From a1ea50d505b3b02ff69c9289877e1f692ee2e679 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Thu, 4 Jun 2020 18:05:34 +0300 Subject: [PATCH 08/12] Added LEETCODE "linked-list-cycle-ii" solution. --- linked-list.md | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/linked-list.md b/linked-list.md index 8902728..c5172a3 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,28 +1,31 @@ # Linked list -+ [Remove Nth Node From End of List](#remove-nth-node-from-end-of-list) ++ [Linked List Cycle II](#linked-list-cycle-ii) -## Remove Nth Node From End of List +## Linked List Cycle II -https://leetcode.com/problems/remove-nth-node-from-end-of-list/ +https://leetcode.com/problems/linked-list-cycle-ii/ ```python # Definition for singly-linked list. # class ListNode(object): -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next -class Solution: - def removeNthFromEnd(self, head, n): - pastNode = nextNode = head - for i in range(n): - nextNode = nextNode.next - if not nextNode: - return head.next - while nextNode.next: - nextNode = nextNode.next - pastNode = pastNode.next - pastNode.next = pastNode.next.next +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def detectCycle(self, _head): + tail = curNode = head = _head + while curNode and curNode.next: + tail = tail.next + curNode = curNode.next.next + if tail is curNode: + break + else: + return None + while head is not tail: + tail = tail.next + head = head.next return head ``` \ No newline at end of file From 644123608e3df1aacd047810fe31a5ed746c9c76 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Thu, 4 Jun 2020 23:59:35 +0300 Subject: [PATCH 09/12] Added LEETCODE "linked-list-cycle" solution. --- linked-list.md | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/linked-list.md b/linked-list.md index c5172a3..038f498 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,10 +1,10 @@ # Linked list -+ [Linked List Cycle II](#linked-list-cycle-ii) ++ [Linked List Cycle](#linked-list-cycle) -## Linked List Cycle II +## Linked List Cycle -https://leetcode.com/problems/linked-list-cycle-ii/ +https://leetcode.com/problems/linked-list-cycle/ ```python # Definition for singly-linked list. @@ -14,18 +14,13 @@ https://leetcode.com/problems/linked-list-cycle-ii/ # self.next = None class Solution(object): - def detectCycle(self, _head): - tail = curNode = head = _head - while curNode and curNode.next: + def hasCycle(self, root): + tail = head = root + while head and head.next: + head = head.next.next tail = tail.next - curNode = curNode.next.next - if tail is curNode: - break - else: - return None - while head is not tail: - tail = tail.next - head = head.next - return head + if tail is head: + return True + return False ``` \ No newline at end of file From 11075915b1b98d1e00320434d0c43613a9a1dd08 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Fri, 5 Jun 2020 00:08:39 +0300 Subject: [PATCH 10/12] Added LEETCODE "reorder-list" solution. --- linked-list.md | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/linked-list.md b/linked-list.md index 038f498..37e03e1 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,26 +1,32 @@ # Linked list -+ [Linked List Cycle](#linked-list-cycle) ++ [Reorder List (deque)](#reorder-list-deque) -## Linked List Cycle +## Reorder List (deque) -https://leetcode.com/problems/linked-list-cycle/ +https://leetcode.com/problems/reorder-list/ ```python # Definition for singly-linked list. # class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next class Solution(object): - def hasCycle(self, root): - tail = head = root - while head and head.next: - head = head.next.next - tail = tail.next - if tail is head: - return True - return False + def reorderList(self, head): + prev,curr = ListNode(0),head + q = collections.deque() + while head: + q.append(head) + head = head.next + + i = 1 + while q: + if i & 1: curr = q.popleft() + else: curr = q.pop() + prev.next = curr + prev = curr + i+=1 + if curr: curr.next = None ``` \ No newline at end of file From a35348ff6642a0782020ff95e2f5057d3015e0ba Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Fri, 5 Jun 2020 00:19:59 +0300 Subject: [PATCH 11/12] Added LEETCODE "intersection-of-two-linked-lists" solution. --- linked-list.md | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/linked-list.md b/linked-list.md index 37e03e1..2210baf 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,32 +1,40 @@ # Linked list -+ [Reorder List (deque)](#reorder-list-deque) ++ [Intersection of Two Linked Lists](#intersection-of-two-linked-lists) -## Reorder List (deque) +## Intersection of Two Linked Lists -https://leetcode.com/problems/reorder-list/ +https://leetcode.com/problems/intersection-of-two-linked-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 reorderList(self, head): - prev,curr = ListNode(0),head - q = collections.deque() - while head: - q.append(head) - head = head.next +# def __init__(self, x): +# self.val = x +# self.next = None - i = 1 - while q: - if i & 1: curr = q.popleft() - else: curr = q.pop() - prev.next = curr - prev = curr - i+=1 - if curr: curr.next = None +class Solution(object): + def getIntersectionNode(self, _headA, _headB): + """ + :type _headA, _headB: ListNode + :rtype: ListNode + """ + headA, headB = _headA, _headB + if not headA or not headB: + return None + curA, curB = headA, headB + flagA, flagB = False, False + while curA is not curB: + if not flagA and not curA.next: + flagA = True + curA = headB + else: + curA = curA.next + if not flagB and not curB.next: + flagB = True + curB = headA + else: + curB = curB.next + return curA ``` \ No newline at end of file From 1a567e7b4bbc40bfc4c4950638a1b853b0bdd9e4 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Fri, 5 Jun 2020 02:18:29 +0300 Subject: [PATCH 12/12] Removed flags. Solution with 2 pointers. --- linked-list.md | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/linked-list.md b/linked-list.md index 2210baf..842583b 100644 --- a/linked-list.md +++ b/linked-list.md @@ -12,7 +12,6 @@ https://leetcode.com/problems/intersection-of-two-linked-lists/ # def __init__(self, x): # self.val = x # self.next = None - class Solution(object): def getIntersectionNode(self, _headA, _headB): """ @@ -20,21 +19,14 @@ class Solution(object): :rtype: ListNode """ headA, headB = _headA, _headB - if not headA or not headB: + if not (headA and headB): return None - curA, curB = headA, headB - flagA, flagB = False, False - while curA is not curB: - if not flagA and not curA.next: - flagA = True - curA = headB - else: - curA = curA.next - if not flagB and not curB.next: - flagB = True - curB = headA - else: - curB = curB.next - return curA + + nodeA, nodeB = headA, headB + while nodeA is not nodeB: + nodeA = headB if not nodeA else nodeA.next + nodeB = headA if not nodeB else nodeB.next + + return nodeA ``` \ No newline at end of file