GeezerVison is showing github.com — Added linked lists solutions · spudmonkey/coding-problems@7e42aeb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Remove Duplicates from Sorted Linked List | | 3 | +
| | 4 | +Given a sorted linked list nums, remove the duplicates in-place such that each element appear only once and return the modified linked list. | | 5 | +Do not allocate extra space for another linked list, you must do this by modifying the input linked list in-place with O(1) extra memory. | | 6 | +
| | 7 | +Input: 1 -> 1 -> 2 | | 8 | +Output: 1 -> 2 | | 9 | +
| | 10 | +Input: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> 3 -> 3 -> 4 | | 11 | +Output: 0 -> 1 -> 2 -> 3 -> 4 | | 12 | +
| | 13 | +========================================= | | 14 | +Iterate the linked list and jump the neighbouring duplicates (change the next pointer). | | 15 | + Time Complexity: O(N) | | 16 | + Space Complexity: O(1) | | 17 | +''' | | 18 | + | | 19 | + | | 20 | +############ | | 21 | +# Solution # | | 22 | +############ | | 23 | + | | 24 | +class ListNode: | | 25 | + def __init__(self, x, n=None): | | 26 | + self.val = x | | 27 | + self.next = n | | 28 | + | | 29 | +def remove_duplicates(nums): | | 30 | + if nums is None: | | 31 | + return nums | | 32 | + pointer = nums | | 33 | + | | 34 | + while pointer.next is not None: | | 35 | + if pointer.val == pointer.next.val: | | 36 | + # skip the next value because it's a duplicate | | 37 | + pointer.next = pointer.next.next | | 38 | + else: | | 39 | + # search next | | 40 | + pointer = pointer.next | | 41 | + | | 42 | + return nums | | 43 | + | | 44 | + | | 45 | +########### | | 46 | +# Testing # | | 47 | +########### | | 48 | + | | 49 | +# Test 1 | | 50 | +# Correct result => 1 -> 2 | | 51 | +ll = ListNode(1, ListNode(1, ListNode(2))) | | 52 | +res = remove_duplicates(ll) | | 53 | +while res is not None: | | 54 | + print(res.val) | | 55 | + res = res.next | | 56 | + | | 57 | +# Test 2 | | 58 | +# Correct result => 0 -> 1 -> 2 -> 3 -> 4 | | 59 | +ll = ListNode(0, ListNode(0, ListNode(1, ListNode(1, ListNode(1, ListNode(2, ListNode(2, ListNode(3, ListNode(3, ListNode(4)))))))))) | | 60 | +res = remove_duplicates(ll) | | 61 | +while res is not None: | | 62 | + print(res.val) | | 63 | + res = res.next |
Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Remove Element | | 3 | +
| | 4 | +Given a linked list nums and a value val, remove all instances of that value in-place and return the new linked list. | | 5 | +Do not allocate extra space for another linked list, you must do this by modifying the input linked list in-place with O(1) extra memory. | | 6 | +
| | 7 | +Input: 3 -> 2 -> 2 -> 3 | | 8 | +Output: 2 -> 2 | | 9 | +
| | 10 | +Input: 0 -> 1 -> 2 -> 2 -> 3 -> 0 -> 4 -> 2 | | 11 | +Output: 0 -> 1 -> 3 -> 0 -> 4 | | 12 | +
| | 13 | +========================================= | | 14 | +Iterate the linked list and jump the values that needs to be deleted (change the next pointer). | | 15 | + Time Complexity: O(N) | | 16 | + Space Complexity: O(1) | | 17 | +''' | | 18 | + | | 19 | + | | 20 | +############ | | 21 | +# Solution # | | 22 | +############ | | 23 | + | | 24 | +class ListNode: | | 25 | + def __init__(self, x, n=None): | | 26 | + self.val = x | | 27 | + self.next = n | | 28 | + | | 29 | +def remove_element(nums, val): | | 30 | + res = ListNode(0) | | 31 | + res.next = nums | | 32 | + pointer = res | | 33 | + | | 34 | + while pointer.next is not None: | | 35 | + if pointer.next.val == val: | | 36 | + # skip the next value because it's value that needs to be deleted | | 37 | + pointer.next = pointer.next.next | | 38 | + else: | | 39 | + # search next | | 40 | + pointer = pointer.next | | 41 | + | | 42 | + return res.next | | 43 | + | | 44 | + | | 45 | +########### | | 46 | +# Testing # | | 47 | +########### | | 48 | + | | 49 | +# Test 1 | | 50 | +# Correct result => 2 -> 2 | | 51 | +ll = ListNode(3, ListNode(2, ListNode(2, ListNode(3)))) | | 52 | +res = remove_element(ll, 3) | | 53 | +while res is not None: | | 54 | + print(res.val) | | 55 | + res = res.next | | 56 | + | | 57 | +# Test 2 | | 58 | +# Correct result => 0 -> 1 -> 3 -> 0 -> 4 | | 59 | +ll = ListNode(0, ListNode(1, ListNode(2, ListNode(3, ListNode(0, ListNode(4, ListNode(2))))))) | | 60 | +res = remove_element(ll, 2) | | 61 | +while res is not None: | | 62 | + print(res.val) | | 63 | + res = res.next |
Collapse file Copy file name to clipboardExpand all lines: Linked Lists/reverse_ll.py +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -70,15 +70,15 @@ def reverse(node, prev_node): | 70 | 70 | # Test 1 | 71 | 71 | # Correct result => 4 -> 3 -> 2 -> 1 | 72 | 72 | ll = ListNode(1, ListNode(2, ListNode(3, ListNode(4)))) | 73 | | -kk = reverse_ll(ll) | 74 | | -while kk is not None: | 75 | | - print(kk.val) | 76 | | - kk = kk.next | | 73 | +res = reverse_ll(ll) | | 74 | +while res is not None: | | 75 | + print(res.val) | | 76 | + res = res.next | 77 | 77 |
| 78 | 78 | # Test 2 | 79 | 79 | # Correct result => 4 -> 3 -> 2 -> 1 | 80 | 80 | ll = ListNode(1, ListNode(2, ListNode(3, ListNode(4)))) | 81 | | -kk = reverse_ll_2(ll) | 82 | | -while kk is not None: | 83 | | - print(kk.val) | 84 | | - kk = kk.next | | 81 | +res = reverse_ll_2(ll) | | 82 | +while res is not None: | | 83 | + print(res.val) | | 84 | + res = res.next |
|