python_lintcode_166 Chain list reciprocal nth node _671 cyclic word __python

Source: Internet
Author: User
Tags lintcode
166 Chain List reciprocal nth node Topic

http://www.lintcode.com/zh-cn/problem/nth-to-last-node-in-list/

Find the reciprocal nth node of a single linked list to ensure that the minimum number of nodes in the list is N.

Example
Gives the list 3->2->1->5->null and n = 2, and returns the value 1 of the penultimate node. train of Thought

-Reverse: From head to bottom n number, get the value code

' "'
Definition of ListNode
class ListNode (object):

    def __init__ (self, Val, next=none):
        self.val = val< C4/>self.next = Next
""
class Solution: "" "
    @param head:the The linked list
    of the A. @param n:an Integer.
    @return: Nth to last node of a singly linked list.
    "" " def nthtolast (self, head, N):
        # Write your code here
        current=head 
        count=0
        #计算链表的节点数
        while Current!=none:
            count+=1
            current=current.next
        x=head
        #得到第count-n-1= Reciprocal nth node for
        i in range (count-n):
            x=x.next return
        x

671 Circulation Words Topic

http://www.lintcode.com/zh-cn/problem/rotate-words/

The words are same rotate words if rotate the word to the right by loop, and get another. Count how many different rotate word sets in dictionary.

e.g. picture and turepic are same rotate words.

Notice

All words are lowercase.

Have you met this question in a real interview? Yes
Example
Given dict = ["Picture", "Turepic", "Icturep", "word", "ORDW", "lint"]
Return 3.

"Picture", "Turepic", "Icturep" are same ratote words.
"word", "ORDW" are same too.
The "lint" is the third word this different from the previous two words. train of thought data is not according to the order of cyclic words, that may be the last word is the first cycle of words, therefore, the need to keep the cycle of the word before the code thinking is very simple, that is, words words and X (all occurrences of the loop word), if the word in X, The counter is count+=1, otherwise, the word's possible looping word is stored in the X code

class Solution: "" "@param: Words:a List of words @return: Return to how many different Rotate words "" "Def countrotatewords (self, words): # Write Your code here If Len (words) <2:ret Urn Len (words) X=self.bu (words[0],[]) count=1 #判断是否是之前出现的循环单词 for I in words:i
    f i in X:continue else:count+=1 X=self.bu (i,x) return count "" Bu () function: The word all the loop results, because the given data is not in order, so it is possible that the last one is the first loop word a represents the newly added word, b for the already stored loop word "" "Def Bu (self,a,b): M =len (a) A=a+a for I in Range (m): B.append (A[i:m+i]) return b 

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.