A golang solution


  • 0
    N
    func rotateRight(head *ListNode, k int) *ListNode {
        if head == nil {
    		return head
    	}
    
    	var count int
    	m := make(map[int]*ListNode)
    
    	for head != nil {
    		m[count] = head
    		count++
    		head = head.Next
    	}
    
    	k = k % count
    	if k == 0 {
    	    return m[0]
    	}
    
    	m[count - 1].Next = m[0]
    	m[count - k - 1].Next = nil
    	return m[count - k]
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.