-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path129_Rehashing.py
More file actions
46 lines (37 loc) · 1.41 KB
/
129_Rehashing.py
File metadata and controls
46 lines (37 loc) · 1.41 KB
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
"""
C++/Java: if you directly calculate -4 % 3 you will get -1. You can use function: a % b = (a % b + b) % b to make it is a non negative integer.
Python: you can directly use -1 % 3, you will get 2 automatically.
Input : [null, 21->9->null, 14->null, null]
Output : [null, 9->null, null, null, null, 21->null, 14->null, null]
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param hashTable: A list of The first node of linked list
@return: A list of The first node of linked list which have twice size
"""
def addListNode(self, num, idx, NewHashTbl):
node = NewHashTbl[idx]
while node.next != None:
node = node.next
node.next = ListNode(num)
def addNode(self, num, NewHashTbl):
idx = num % len(NewHashTbl)
if NewHashTbl[idx] == None:
NewHashTbl[idx] = ListNode(num)
else:
self.addListNode(num, idx, NewHashTbl)
def rehashing(self, hashTable):
if len(hashTable) == 0:
return []
size = len(hashTable) * 2
NewHashTbl = [None] * size
for node in hashTable:
while node != None:
self.addNode(node.val, NewHashTbl) # use "node.val" instead of node because node.next is uncertain
node = node.next
return NewHashTbl