-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRehashing.cpp
More file actions
28 lines (28 loc) · 841 Bytes
/
Rehashing.cpp
File metadata and controls
28 lines (28 loc) · 841 Bytes
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
class Solution {
public:
/**
* @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
*/
vector<ListNode*> rehashing(vector<ListNode*> hashTable) {
// write your code here
int n = hashTable.size();
if(n == 0) return {};
int m = n*2;
vector<ListNode*> ans(m, NULL), tail(m, NULL);
for(ListNode* h:hashTable) {
while(h) {
ListNode* t = h->next;
h->next = NULL;
int a = (h->val%m+m)%m;
if(tail[a] == NULL) ans[a] = tail[a] = h;
else {
tail[a]->next = h;
tail[a] = h;
}
h = t;
}
}
return ans;
}
};