add new solutions
This commit is contained in:
parent
d798d5e8c9
commit
886b5e0a8e
34 changed files with 1164 additions and 0 deletions
35
solutions/0/q61/solution.go
Normal file
35
solutions/0/q61/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package q61
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func rotateRight(head *ListNode, k int) *ListNode {
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
n := 1
|
||||
p := head
|
||||
for p.Next != nil {
|
||||
p = p.Next
|
||||
n++
|
||||
}
|
||||
tail := p
|
||||
|
||||
k %= n
|
||||
if k == 0 {
|
||||
return head
|
||||
}
|
||||
|
||||
tail.Next = head
|
||||
p = head
|
||||
for range n - k - 1 {
|
||||
p = p.Next
|
||||
}
|
||||
newHead := p.Next
|
||||
p.Next = nil
|
||||
return newHead
|
||||
}
|
||||
|
||||
var _ = rotateRight
|
||||
Loading…
Add table
Add a link
Reference in a new issue