Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4c1b370

Browse files
committed
day 2: more python solutions
1 parent 55dd617 commit 4c1b370

File tree

5 files changed

+222
-10
lines changed

5 files changed

+222
-10
lines changed

‎README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
| Current Status| Stats |
88
| :------------: | :----------: |
99
| Total Problems | 188 |
10-
| Current Daily Streak| 1 |
11-
| Last updated | 06/20/2019|
10+
| Current Daily Streak| 2 |
11+
| Last updated | 06/21/2019|
1212

1313
</center>
1414

@@ -17,12 +17,12 @@
1717
### LinkedList Problems
1818
| Problem | Solution |
1919
| :------------ | :----------: |
20-
| Find the nth node of linked list from last. |[nthToLastNode.cpp](linked_list_problems/nthToLastNode.cpp) |
21-
| Add numbers where each digit of the number is represented by node of a linkedlist. Give output as a linked list. | [add_two_numbers_lists.cpp](linked_list_problems/add_two_numbers_lists.cpp)|
20+
| Find the nth node of linked list from last. |[nthToLastNode.cpp](linked_list_problems/nthToLastNode.cpp), [nth_to_last_node.py](linked_list_problems/nth_to_last_node.py) |
21+
| Add numbers where each digit of the number is represented by node of a linkedlist. Give output as a linked list. | [add_two_numbers_lists.cpp](linked_list_problems/add_two_numbers_lists.cpp), [add_two_numbers_list.py](linked_list_problems/add_two_numbers_lists.py)|
2222
| Swap nodes of a linkedlist without swapping data. |[swapNodesWithoutSwappingData.cpp](linked_list_problems/swapNodesWithoutSwappingData.cpp)|
2323
| Reverse a linked list, iteratively and recursively | [reverseLinkedListIterAndRecurse.cpp](linked_list_problems/reverseLinkedListIterAndRecurse.cpp)|
2424
| Given a linked list, reverse alternate nodes and append at the end. | [reverseAlternateNodes.cpp](linked_list_problems/reverseAlternateNodes.cpp) |
25-
| Only given a node pointer, delete the node from the linked list. | [deleteNode.cpp](linked_list_problems/deleteNode.cpp)|
25+
| Only given a node pointer, delete the node from the linked list. | [deleteNode.cpp](linked_list_problems/deleteNode.cpp)|
2626
| Delete the entire linkedlist. | [deleteLinkedlist.cpp](linked_list_problems/deleteLinkedlist.cpp)|
2727
| Print middle node of linkedlist without iterating twice. | [printMiddleNode.cpp](linked_list_problems/printMiddleNode.cpp) | | Detecting and removing a cycle in linkedlist.| [floyedCycleDetection.cpp](linked_list_problems/floyedCycleDetection.cpp)|
2828
| Determine if a linked list is a pallindrome. | [listPallindrome.cpp](linked_list_problems/listPallindrome.cpp) |

‎linked_list_problems/add_two_numbers_lists.cpp‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,5 @@ int main()
103103
iterate(list2);
104104
ListNode *list3 = addLists(list1, list2);
105105
iterate(list3);
106-
107-
108-
109-
110106
return 0;
111-
112107
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
We have two numbers represented by a linked list,
3+
where each node contains a single digit. The digits are stored in reverse order,
4+
such that the 1's digit is at the head of the list.
5+
Write a function that adds the two numbers and returns the sum as a linked list.
6+
Example:
7+
8+
Given 7->1->6 + 5->9->2. That is, 617 + 295.
9+
Return 2->1->9. That is 912.
10+
11+
Given 3->1->5 and 5->9->2, return 8->0->8.
12+
"""
13+
14+
class Node:
15+
"""Representation of a linked list node"""
16+
def __init__(self, data):
17+
self.data = data
18+
self.next = None
19+
20+
def add_two_numbers_lists(list1, list2):
21+
""" Adds two numbers represented as a linked list and
22+
returns the sum as a linked list
23+
Example:
24+
7->1->6 + 5->9->2 = 2->1->9
25+
(617 + 295 = 912)
26+
Params:
27+
list1 : list representing first number
28+
list2 : list representing second number
29+
"""
30+
list3 = None
31+
prev = None
32+
carry = 0
33+
while list1 is not None or list2 is not None:
34+
res = carry
35+
res += 0 if list1 is None else list1.data
36+
res += 0 if list2 is None else list2.data
37+
carry = 1 if res >= 10 else 0
38+
res = res % 10
39+
newNode = Node(res)
40+
41+
if list3 is None:
42+
list3 = newNode
43+
else:
44+
prev.next = newNode
45+
46+
prev = newNode
47+
48+
if list1 is not None:
49+
list1 = list1.next
50+
if list2 is not None:
51+
list2 = list2.next
52+
53+
return list3
54+
55+
def print_list(head):
56+
while head:
57+
if head.next is None:
58+
print(head.data)
59+
else:
60+
print(str(head.data)+ '-->', end = ' ')
61+
head = head.next
62+
63+
64+
if __name__ == '__main__':
65+
print("List 1:")
66+
list1 = Node(7)
67+
list1.next = Node(1)
68+
list1.next.next = Node(6)
69+
print_list(list1)
70+
71+
print("List 2:")
72+
list2 = Node(5)
73+
list2.next = Node(9)
74+
list2.next.next = Node(2)
75+
print_list(list2)
76+
77+
print("Result List:")
78+
list3 = add_two_numbers_lists(list1, list2)
79+
print_list(list3)
80+
81+
print("Example 2: ")
82+
print("List 1:")
83+
list1 = Node(3)
84+
list1.next = Node(1)
85+
list1.next.next = Node(5)
86+
print_list(list1)
87+
88+
print("List 2:")
89+
list2 = Node(5)
90+
list2.next = Node(9)
91+
list2.next.next = Node(2)
92+
print_list(list2)
93+
94+
print("Result List:")
95+
list3 = add_two_numbers_lists(list1, list2)
96+
print_list(list3)

‎linked_list_problems/delete_node.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Problem: Only given a node of the linked list, delete the node.
2+
3+
4+
class Node:
5+
"""Representation of a node of a linked list"""
6+
def __init__(self, data):
7+
self.data = data
8+
self.next = None
9+
10+
class LinkedList:
11+
"""Representation of a linked list"""
12+
def __init__(self):
13+
self.head = None
14+
15+
def push(self, data):
16+
"""Pushes a new node to the linkedlist, the new node becomes head
17+
Params:
18+
data of the new node.
19+
"""
20+
new_node = Node(data)
21+
new_node.next = self.head
22+
self.head = new_node
23+
24+
def print_list(self):
25+
"""Prints the linked list"""
26+
itr = self.head
27+
while itr:
28+
if itr.next is None:
29+
print(itr.data)
30+
else:
31+
print(str(itr.data)+ "-->", end=" ")
32+
itr = itr.next
33+
34+
def delete_node(self, node):
35+
"""Deletes a given node from the linked list
36+
Params:
37+
node: node to be deleted
38+
"""
39+
itr = self.head
40+
while itr.next != node:
41+
itr = itr.next
42+
if itr is not None:
43+
itr.next = node.next
44+
45+
node.next = None
46+
47+
def get_head(self):
48+
return self.head
49+
50+
51+
if __name__ == '__main__':
52+
linkedList = LinkedList()
53+
linkedList.push(1)
54+
linkedList.push(2)
55+
linkedList.push(3)
56+
linkedList.push(4)
57+
linkedList.push(4)
58+
print("List before deleting:")
59+
linkedList.print_list()
60+
61+
# Delete second node from the list.
62+
linkedList.delete_node(linkedList.get_head().next)
63+
print("List after deleting:")
64+
linkedList.print_list()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Find nth to last node in a linked list.
3+
"""
4+
5+
class Node:
6+
def __init__(self, data):
7+
"""Representation of a linked list node"""
8+
self.data = data
9+
self.next = None
10+
11+
12+
def nth_to_last(head, n):
13+
""" Find nth to last node of a linked list
14+
Params:
15+
head : head of the linked list
16+
n: the distance of target node from the end of linkedlist.
17+
"""
18+
itr1 = head
19+
itr2 = head
20+
i = 1
21+
22+
# First move the first iterator to nth node from beginning.
23+
while itr1 and i <= n:
24+
itr1 = itr1.next
25+
i += 1
26+
27+
# Now move the second iterator from head, till the first iterator reaches end.
28+
# If we need to find nth node from the last, we need to find l-nth node from the
29+
# beginning, where l is the length of the list.
30+
while itr1:
31+
itr1 = itr1.next
32+
itr2 = itr2.next
33+
34+
return itr2.data
35+
36+
37+
def print_list(head):
38+
""" Prints a linked list.
39+
Params:
40+
head: head of the linked list
41+
"""
42+
while head:
43+
if not head.next:
44+
print(head.data)
45+
else:
46+
print(str(head.data) + " --> ", end = " ")
47+
head = head.next
48+
49+
50+
if __name__ == "__main__":
51+
root = Node(3)
52+
root.next = Node(2)
53+
root.next.next = Node(1)
54+
root.next.next.next = Node(5)
55+
root.next.next.next.next = Node(4)
56+
print_list(root)
57+
print("4th node from the last of the list is: ", nth_to_last(root, 4))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /