summaryrefslogtreecommitdiff
path: root/2020
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-08-15 10:27:05 -0600
committermo khan <mo.khan@gmail.com>2020-08-15 10:27:05 -0600
commit5143fd22d32aad6f5c93f044b996806aadc3df59 (patch)
tree986225b9934fe18ca4125b1b124d80e22b89fcb6 /2020
parentf603081da67aaa371f1db31f3b1dac0d5dbb3ca4 (diff)
Add daily problem
Diffstat (limited to '2020')
-rw-r--r--2020/08/15/README.md47
1 files changed, 47 insertions, 0 deletions
diff --git a/2020/08/15/README.md b/2020/08/15/README.md
new file mode 100644
index 0000000..c4df310
--- /dev/null
+++ b/2020/08/15/README.md
@@ -0,0 +1,47 @@
+│ Given a singly-linked list, reverse the list. This can be done
+│ iteratively or recursively. Can you get both solutions?
+│ Example:
+│Input: 4 -> 3 -> 2 -> 1 -> 0 -> NULL
+│Output: 0 -> 1 -> 2 -> 3 -> 4 -> NULL
+│
+│class ListNode(object):
+│ def __init__(self, x):
+│ self.val = x
+│ self.next = None
+│
+│ # Function to print the list
+│ def printList(self):
+│ node = self
+│ output = ''
+│ while node != None:
+│ output += str(node.val)
+│ output += " "
+│ node = node.next
+│ print(output)
+│ # Iterative Solution
+│ def reverseIteratively(self, head):
+│ # Implement this.
+│ # Recursive Solution
+│ def reverseRecursively(self, head):
+│ # Implement this.
+│# Test Program
+│# Initialize the test list:
+│testHead = ListNode(4)
+│node1 = ListNode(3)
+│testHead.next = node1
+│node2 = ListNode(2)
+│node1.next = node2
+│node3 = ListNode(1)
+│node2.next = node3
+│testTail = ListNode(0)
+│node3.next = testTail
+│print("Initial list: ")
+│testHead.printList()
+│# 4 3 2 1 0
+│testHead.reverseIteratively(testHead)
+│#testHead.reverseRecursively(testHead)
+│print("List after reversal: ")
+│testTail.printList()
+│# 0 1 2 3 4
+
+