From 42fb596ac01416f4a32db850222606613979a5fd Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Wed, 13 May 2020 20:53:05 +0300 Subject: [PATCH 1/8] +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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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