LintCode:重哈希

LintCode:重哈希

""" 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 rehashing(self, hashTable):
        # write your code here
        m = len(hashTable)
        ans = [None for i in range(2 * m)]
        for head in hashTable:
            if head == None:
                pass
            else:
                p1 = head
                while p1 != None:
                    n = p1.val % (2 * m)
                    if ans[n] == None:
                        ans[n] = ListNode(p1.val)
                    else:
                        p2 = ans[n]
                        tmp_node = ListNode(p1.val)
                        while p2.next != None:
                            p2 = p2.next
                        p2.next = tmp_node
                    p1 = p1.next
        return ans

你可能感兴趣的:(LintCode:重哈希)