-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy path061-RotateList.cs
More file actions
37 lines (32 loc) · 947 Bytes
/
061-RotateList.cs
File metadata and controls
37 lines (32 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//-----------------------------------------------------------------------------
// Runtime: 84ms
// Memory Usage: 26 MB
// Link: https://leetcode.com/submissions/detail/408819041/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _061_RotateList
{
public ListNode RotateRight(ListNode head, int k)
{
if (k <= 0 || head == null) { return head; }
var ptr = new ListNode(-1);
ptr.next = head;
int lenght = 0;
while (ptr.next != null)
{
ptr = ptr.next;
lenght++;
}
ptr.next = head;
var rest = lenght - k % lenght;
for (int i = 0; i < rest; i++)
{
ptr = ptr.next;
}
head = ptr.next;
ptr.next = null;
return head;
}
}
}