Leetcode intersect linked list Python implementation

Source: Internet
Author: User

It is not difficult to solve this problem, open two-layer loop to traverse can be achieved, but will time out

If I wanted the time complexity of O (n), I would consider using a hash table to store the traversed element, and if I found the currently traversed element in the hash table, that means the intersection is here.

This takes advantage of the hash table lookup time is O (1)

However, this algorithm does not meet the requirement of O (1) for space complexity.

The code looks like this:

1 classsolution (object):2     defGetintersectionnode (self, Heada, headb):3         """4 : Type Head1, Head1:listnode5 : Rtype:listnode6         """7         #If there's a list that's empty, there's no crossover.8         ifHeada isNoneorHeadb isNone:9             returnNoneTen         #calculate the length of the two linked list OneCA =Heada ACB =headb -TA, tb = 1, 1 -          whileCa.next is  notNone: theTa + = 1 -CA =Ca.next -          whileCb.next is  notNone: -TB + = 1 +CB =Cb.next -         #if the last element is not the same, it means no cross . +         ifCa.val! =Cb.val: A             returnNone at         #if the length of the two linked list is not the same, the linked list pointer will be moved back -CA =Heada -CB =headb -          whileTB >TA: -TB-= 1 -CB =Cb.next in          whileTa >TB: -Ta-= 1 toCA =Ca.next +         #starting from the beginning of a short list with the same countdown length , a comparison -          whileCa is  notNone andCb is  notNone: the             ifCa.val = =Cb.val: *                 returnCA $CA =Ca.nextPanax NotoginsengCB = Cb.next

After a time of thinking, if you want to meet the space complexity is O (1) then can not open up new space, smart pointer to move back and forth the way

1 if any one of the linked lists is empty the description does not intersect

2 If the list crosses the last element must be the same, if not the same description does not cross

3 If there is a crossover, calculate the length of the two linked list,

The thought is adjusted to the same length, and then a bitwise comparison such a traversal can

Specifically: Each linked list has a traversal pointer,

Short list of pointers in the head

The long linked list pointer moves back several bits, until two lists from the tail count to the length of the Traverse pointer are the same

The next step is to iterate over each bit to compare whether it is equal

So without opening up new space time is traversing O (n) over and over

1 classsolution (object):2     defGetintersectionnode (self, Heada, headb):3         """4 : Type Head1, Head1:listnode5 : Rtype:listnode6         """7         #set up a hash table to store each element that was first traversed8         #if the current element description is crossed before it is found9DIC = {}TenCA =Heada OneCB =headb A          whileCa is  notNoneorCb is  notNone: -             ifCa is  notNone: -                 Try: the Dic[ca.val] -                     returnCA -                 except: -Dic[ca.val] =True +CA =Ca.next -             ifCb is  notNone: +                 Try: A Dic[cb.val] at                     returnCB -                 except: -Dic[cb.val] =True -CB =Cb.next -         returnNone

Leetcode intersect linked list Python implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.