From 42fb596ac01416f4a32db850222606613979a5fd Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Wed, 13 May 2020 20:53:05 +0300 Subject: [PATCH 1/9] +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/9] 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/9] 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/9] 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/9] 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/9] 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 07c4497e92021b22788afa49ff61cca8a9d92acb Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Tue, 2 Jun 2020 00:52:21 +0300 Subject: [PATCH 7/9] Added LEETCODE Merge Two Sorted Lists (iterative) solution. --- linked-list.md | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/linked-list.md b/linked-list.md index f350957..0a314c7 100644 --- a/linked-list.md +++ b/linked-list.md @@ -1,8 +1,9 @@ # Linked list -+ [Merge Two Sorted Lists](#merge-two-sorted-lists) ++ [Merge Two Sorted Lists (recursive)](#merge-two-sorted-lists-recursive) ++ [Merge Two Sorted Lists (iterative)](#merge-two-sorted-lists-iterative) -## Merge Two Sorted Lists +## Merge Two Sorted Lists (recursive) https://leetcode.com/problems/merge-two-sorted-lists/ @@ -20,4 +21,28 @@ class Solution(object): a.next = self.mergeTwoLists(a.next, b) return a +``` + +## Merge Two Sorted Lists (iterative) + +```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 + tail.next = a or b + return head.next + ``` \ No newline at end of file From 5d394cc134c1c53905cb57aefa8de78d0cedde4e Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Tue, 2 Jun 2020 00:55:30 +0300 Subject: [PATCH 8/9] Added a forgotten link for an iterative solution. --- linked-list.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/linked-list.md b/linked-list.md index 0a314c7..6ec28f1 100644 --- a/linked-list.md +++ b/linked-list.md @@ -25,6 +25,8 @@ class Solution(object): ## Merge Two Sorted Lists (iterative) +https://leetcode.com/problems/merge-two-sorted-lists/ + ```python # Definition for singly-linked list. # class ListNode(object): From cc3a76ba49f835c13411fac25b4aad39b40c7291 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Tue, 2 Jun 2020 13:08:43 +0300 Subject: [PATCH 9/9] Changed tail.next = a or b --- linked-list.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/linked-list.md b/linked-list.md index 6ec28f1..cb08825 100644 --- a/linked-list.md +++ b/linked-list.md @@ -44,7 +44,10 @@ class Solution(object): tail.next = b b = b.next tail = tail.next - tail.next = a or b + if a: + tail.next = a + else: + tail.next = b return head.next ``` \ No newline at end of file