-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython160.py
106 lines (70 loc) · 2.68 KB
/
python160.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
### with hashmap
hashmap = {}
currentA = headA
while currentA:
hashmap[currentA] = currentA.val
currentA = currentA.next
currentB = headB
while currentB:
if currentB in hashmap.keys():
return currentB
currentB = currentB.next
### x in list -> O(n)
### x in dict -> O(1)
## time out
# hashmap = []
# i = 0
# currentA = headA
# while currentA:
# hashmap.append(currentA)
# currentA = currentA.next
# i += 1
# currentB = headB
# while currentB:
# if currentB in hashmap:
# return currentB
# # if currentB == hashmap[currentB.val]:
# # return currentB
# currentB = currentB.next
## Time limit exceeded
# hashmap = {}
# i = 0
# currentA = headA
# while currentA:
# hashmap[i] = currentA
# currentA = currentA.next
# i += 1
# currentB = headB
# while currentB:
# for i in range(len(hashmap)):
# if currentB == hashmap[i]:
# return currentB
# currentB = currentB.next
## Time limit exceeded O(NxM) complexity (i guess)
# currentA = headA
# while currentA:
# currentB = headB # Start again from headB
# while currentB: # Keeping one linkedlist at one node while iterating the other node
# if currentA == currentB: # If the nodes intersect
# return currentA
# currentB = currentB.next
# currentA = currentA.next
##Time limit exceeded
# currentA = headA
# currentB = headB
# while True:
# if currentA == currentB:
# return currentA
# currentA = currentA.next if currentA else headA
# currentB = currentB.next if currentB else headB
# if currentA == headA and currentB == headB:
# break
# else:
# continue