-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
69 lines (61 loc) · 1.39 KB
/
test.cpp
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
#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if (head==NULL) return head;
ListNode *Head=head;
int len=0;
ListNode *cur=head;
while (cur!=NULL){
cur=cur->next;
len++;
}
k=k%len;
if (k==0) return head;
//找到倒数第k个节点
cur=head;
int num=len-k;
while (cur!=NULL){
num--;
if (num==0) {
break;
}
cur=cur->next;
}
ListNode* lasthead=cur->next;
ListNode* lastcur=lasthead;
cur->next=NULL;
while (lastcur!=NULL){
if (lastcur->next==NULL) break;
lastcur=lastcur->next;
}
lastcur->next=head;
return lasthead;
}
};
int main(){
ListNode* node1=new ListNode(0);
ListNode* node2=new ListNode(1);
ListNode* node3=new ListNode(2);
ListNode* node4=new ListNode(4);
node1->next=node2;
node2->next=node3;
//node3->next=node4;
ListNode* cur=node1;
while (cur){
cout<<cur->val<<' ';
cur=cur->next;
}
Solution sol;
}