将给定的链表向右转动k个位置,k是非负数。
例如:
给定1->2->3->4->5->null , k=2,
返回4->5->1->2->3->null。
class ListNode: def __init__(self, x): self.val = x self.next = None class Link: def __init__(self): self.head = None def addlink(self,val): node1=ListNode(val) if self.head==None: self.head=node1 return else: cur=self.head while(cur.next): cur=cur.next cur.next=node1 return # # # @param head ListNode类 # @param k int整型 # @return ListNode类 # class Solution: def rotateRight(self, head, k): # write code here if head == None&nbs***bsp;k == 0: return head totallist=[] while(head): totallist.append(head.val) head=head.next while(k): totallist.insert(0,totallist.pop()) k-=1 l1=Link() for i in totallist: l1.addlink(i) return l1.head