forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrehashing.py
More file actions
38 lines (37 loc) · 1.43 KB
/
rehashing.py
File metadata and controls
38 lines (37 loc) · 1.43 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
# -*- coding: utf-8 -*-
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 rehashing(self, hashTable):
# write your code here
old_size = len(hashTable)
for i in xrange(old_size):
hashTable.append(None)
for i in xrange(old_size):
prev, node = None, hashTable[i]
while node:
pos = node.val % (old_size * 2)
if pos >= old_size: # 该元素需要分配到新的位置上
next_node = node.next # 先保留next节点
if not hashTable[pos]:
hashTable[pos] = node
else:
new_node = hashTable[pos]
while new_node:
if new_node.next:
new_node = new_node.next # 插到新链表的尾部
else:
new_node.next = node
break
node.next = None
if not prev:
hashTable[i] = next_node
else:
prev.next = next_node
node = next_node
else:
prev = node
node = node.next
return hashTable