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