Skip to content

Commit 88c4e7e

Browse files
author
Мето Трајковски
committed
Added a solution for reversing Linked Lists
1 parent f10f9a8 commit 88c4e7e

File tree

5 files changed

+92
-7
lines changed

5 files changed

+92
-7
lines changed

Backtracking/jumping_numbers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Make a tree (DFS way/backtracking), for each next digit take the last digit, go up and down
1414
(example: 123, last digit is 3, so next digit should be 2 or 4).
1515
Time Complexity: O(9 * 2^(NumOfDigits(N) - 1))
16-
Space Complexity: O(1)
16+
Space Complexity: O(1) , recursion stack will have depth 9 (but this can be considered as constant)
1717
'''
1818

1919

@@ -39,11 +39,11 @@ def jumping_num(num, x, result):
3939
last_digit = num % 10
4040
next_num = num * 10
4141

42-
# go down
42+
# decrease the last digit by one
4343
if last_digit != 0:
4444
jumping_num(next_num + last_digit - 1, x, result)
4545

46-
# go up
46+
# increase the last digit by one
4747
if last_digit != 9:
4848
jumping_num(next_num + last_digit + 1, x, result)
4949

Linked Lists/reverse_ll.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'''
2+
Reverse a linked list
3+
4+
Reverse a linked list in one iteration without using additional space.
5+
6+
Input: 1 -> 2 -> 3 -> 4
7+
Output: 4 -> 3 -> 2 -> 1
8+
9+
=========================================
10+
Iterate LL and change the pointer of the current nodes to point to the previous nodes.
11+
Time Complexity: O(N)
12+
Space Complexity: O(1)
13+
Solution 2: Same approach using recursion.
14+
Time Complexity: O(N)
15+
Space Complexity: O(N) , because of the recursion stack (the stack will be with N depth till the last node of the linked list is reached)
16+
17+
'''
18+
19+
20+
############
21+
# Solution #
22+
############
23+
24+
class ListNode:
25+
def __init__(self, v, n=None):
26+
self.val = v
27+
self.next = n
28+
29+
def reverse_ll(ll):
30+
prev_node = None
31+
32+
while ll is not None:
33+
# save the current node
34+
current = ll
35+
# go to the next node
36+
ll = ll.next
37+
38+
# change the pointer of the current node to point to the previous node
39+
current.next = prev_node
40+
# save the current node for the next iteration
41+
prev_node = current
42+
43+
return prev_node
44+
45+
##############
46+
# Solution 2 #
47+
##############
48+
49+
def reverse_ll_2(ll):
50+
return reverse(ll, None)
51+
52+
def reverse(node, prev_node):
53+
if node is None:
54+
# the end of the ll is reached, return the previous node
55+
# that'll be the first node in the reversed ll
56+
return prev_node
57+
58+
# send node.next as current node and node as previous node in the next step
59+
result = reverse(node.next, node)
60+
# change the pointer of the current node to point to the previous node
61+
node.next = prev_node
62+
63+
return result
64+
65+
66+
###########
67+
# Testing #
68+
###########
69+
70+
# Test 1
71+
# Correct result => 4 -> 3 -> 2 -> 1
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
77+
78+
# Test 2
79+
# Correct result => 4 -> 3 -> 2 -> 1
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

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,13 @@ If the problems from [LeetCode](https://leetcode.com/) are not enough and you ne
130130
- [Topcoder](http://topcoder.com/)
131131
- [Project Euler](https://projecteuler.net/)
132132
- [Codility](https://codility.com/)
133-
- [Daily Coding Problem](https://www.dailycodingproblem.com/)
134133
- [CoderByte](https://coderbyte.com/)
135134
- [CodingBat](http://codingbat.com/)
136135
- [CodeAbbey](http://codeabbey.com/)
137136
- [Wolfram Challenges](https://challenges.wolfram.com/)
138137
- [CS Academy](https://csacademy.com/)
138+
- [Daily Coding Problem](https://www.dailycodingproblem.com/)
139+
- [Daily Interview Pro](http://dailyinterviewpro.com/)
139140
- [Codewars](http://www.codewars.com/)
140141
- [SPOJ](http://www.spoj.com/)
141142
- [Online Judge](https://onlinejudge.org/)

Trees/find_second_largest_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
=========================================
77
Traverse tree and compare the current value with the saved 2 values.
88
Time Complexity: O(N)
9-
Space Complexity: O(1)
9+
Space Complexity: O(LogN) , because of the recursion stack (but this is if the tree is balanced), worst case O(N) if the tree is one branch.
1010
'''
1111

1212

Trees/find_second_largest_node_bst.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
=========================================
77
There are 4 possible cases (see the details in the code).
88
Only 1 branch is searched to the end (leaf), not the whole tree.
9-
Time Complexity: O(LogN) , if balanced bst, worst case if all elements are in the right branch O(N)
10-
Space Complexity: O(1)
9+
Time Complexity: O(LogN) , this is if the tree is balanced (balanced bst), but the worst case will be if all elements are in the one (the right) branch O(N)
10+
Space Complexity: O(LogN) , -||- but this is because of the recursion stack (LogN elements will be in the recursion stack till the leaf is reached)
1111
'''
1212

1313

0 commit comments

Comments
 (0)