-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (77 loc) · 2.06 KB
/
Copy pathProgram.cs
File metadata and controls
89 lines (77 loc) · 2.06 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
namespace algorithms;
using algorithms.Extensions;
using algorithms.LeetCode;
internal class Program
{
// h:1 2 s:3 4 f:5
// 4 5 1 2 3
// h:s:1 f:2
// first.next = head
// 1 2 1 ...
// head = second.next
// h:,s.n:,f:2 f.n:1 ...
// second.next = null
//
static void Main(string[] args)
{
new TopKFrequentWords().Test();
}
public static ListNode RotateRight(ListNode head, int k) {
if(head?.next is null) return head;
var n = 1;
var first = new ListNode(head.val, head.next);
var second = new ListNode(head.val, head.next);
while(first.next is not null)
{
first = first.next;
n++;
}
int i = n > k ? n - k - 1 : n - (n%2);
//int i = n - k - 1 > 0 ? n - k - 1 : n + (n - k );
Console.WriteLine($"n = {n} i = {i}");
//return head;
if(i > 0)
{
while(i > 0)
{
second = second.next ?? head;
i--;
}
}
else
{
first.next = head;
}
Console.WriteLine($"first");
first.PrintAll();
Console.WriteLine($"second");
second.PrintAll();
first.next = head;
head = second.next;
second.next = null;
return head;
}
// Definition for singly-linked list.
public class ListNode {
public int val;
public ListNode next;
public ListNode(int val=0, ListNode next=null) {
this.val = val;
this.next = next;
}
public void Print()
{
Console.WriteLine($"val: {this.val}");
}
public void PrintAll()
{
var a = new ListNode(this.val, this.next);
do{
Console.Write($"val: {a.val}");
if(a.next is not null) Console.Write("\t");
a = a.next;
}while(a is not null);
Console.WriteLine();
}
}
}