# Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution(object): defrotateRight(self, head, k): """ :type head: ListNode :type k: int :rtype: ListNode """ ifnot head or k == 0: return head length = 1 current = head while current.next: length += 1 current = current.next k = k % length if k == 0: return head fast = head slow = head for _ inrange(k): fast = fast.next while fast.next: fast = fast.next slow = slow.next new_head = slow.next slow.next = None fast.next = head return new_head
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defrotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: ifnot head or k == 0: return head length = 1 current = head while current.next: length += 1 current = current.next k = k % length if k == 0: return head fast = head slow = head for _ inrange(k): fast = fast.next while fast.next: fast = fast.next slow = slow.next new_head = slow.next slow.next = None fast.next = head return new_head
/** * Definition for singly-linked list. * public class ListNode { * public var val: Int * public var next: ListNode? * public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val; self.next = nil; } * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } * } */ classSolution { funcrotateRight(_head: ListNode?, _k: Int) -> ListNode? { guardlet head = head, k >0else { return head } var length =1 var current = head while current.next !=nil { length +=1 current = current.next! } let rotateSteps = k % length if rotateSteps ==0 { return head } var fast: ListNode? = head var slow: ListNode? = head for_in0..<rotateSteps { fast = fast?.next } while fast?.next !=nil { fast = fast?.next slow = slow?.next } let newHead = slow?.next slow?.next =nil fast?.next = head return newHead } }
/** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ classSolution { funrotateRight(head: ListNode?, k: Int): ListNode? { if (head == null || k == 0) { return head } var length = 1 var current = head while (current?.next != null) { length++ current = current.next } val rotateSteps = k % length if (rotateSteps == 0) { return head } var fast: ListNode? = head var slow: ListNode? = head repeat(rotateSteps) { fast = fast?.next } while (fast?.next != null) { fast = fast?.next slow = slow?.next } val newHead = slow?.next slow?.next = null fast?.next = head return newHead } }
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ funcrotateRight(head *ListNode, k int) *ListNode { if head == nil || k == 0 { return head } length := 1 current := head for current.Next != nil { length++ current = current.Next } k = k % length if k == 0 { return head } fast, slow := head, head for i := 0; i < k; i++ { fast = fast.Next } for fast.Next != nil { fast = fast.Next slow = slow.Next } newHead := slow.Next slow.Next = nil fast.Next = head return newHead }
# Definition for singly-linked list. # class ListNode # attr_accessor :val, :next # def initialize(val = 0, _next = nil) # @val = val # @next = _next # end # end # @param {ListNode} head # @param {Integer} k # @return {ListNode} defrotate_right(head, k) return head if head.nil? || k.zero? length = 1 current = head while current.next length += 1 current = current.next end k = k % length return head if k.zero? fast = head slow = head k.times { fast = fast.next } while fast.next fast = fast.next slow = slow.next end new_head = slow.next slow.next = nil fast.next = head new_head end
/** * Definition for singly-linked list. * class ListNode(_x: Int = 0, _next: ListNode = null) { * var next: ListNode = _next * var x: Int = _x * } */ objectSolution{ defrotateRight(head: ListNode, k: Int): ListNode = { if (head == null || k == 0) { return head } var length = 1 var current = head while (current.next != null) { length += 1 current = current.next } val rotateSteps = k % length if (rotateSteps == 0) { return head } var fast: ListNode = head var slow: ListNode = head for (_ <- 0 until rotateSteps) { fast = fast.next } while (fast.next != null) { fast = fast.next slow = slow.next } val newHead: ListNode = slow.next slow.next = null fast.next = head newHead } }