Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita502841

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parenta1c8649 commita502841

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

‎0061_rotate_list/rotate_list.c‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
#include<stdio.h>
22
#include<stdlib.h>
33

4+
45
structListNode {
56
intval;
67
structListNode*next;
78
};
89

9-
staticstructListNode*rotateRight(structListNode*head,intk)
10+
structListNode*rotateRight(structListNode*head,intk)
1011
{
11-
if (head==NULL||k <=0) {
12+
if (head==NULL) {
1213
returnhead;
1314
}
1415

16+
intlen=0;
1517
structListNodedummy;
1618
dummy.next=head;
17-
structListNode*prev=&dummy;
18-
structListNode*p=head;
19-
intlen=0;
20-
while (p!=NULL) {
21-
prev=p;
22-
p=p->next;
19+
structListNode*tail=&dummy;
20+
while (tail->next!=NULL) {
21+
tail=tail->next;
2322
len++;
2423
}
2524

26-
structListNode*last=prev;
27-
prev=&dummy;
28-
p=head;
25+
structListNode*prev=&dummy;
26+
structListNode*p=head;
2927
len=len- (k %len);
3028
while (len-->0) {
3129
prev=p;
@@ -36,23 +34,22 @@ static struct ListNode* rotateRight(struct ListNode* head, int k)
3634
/* deletion */
3735
prev->next=NULL;
3836
/* insertion */
39-
last->next=dummy.next;
40-
dummy.next=p;
37+
tail->next=head;
38+
head=p;
4139
}
4240

43-
returndummy.next;
41+
returnhead;
4442
}
4543

4644
intmain(intargc,char**argv)
4745
{
48-
inti;
49-
structListNode*p,*prev,dummy,*list;
50-
5146
if (argc<2) {
5247
fprintf(stderr,"Usage: ./test k n1 n2...\n");
5348
exit(-1);
5449
}
5550

51+
inti;
52+
structListNode*p,*prev,dummy,*list;
5653
dummy.next=NULL;
5754
prev=&dummy;
5855
for (i=2;i<argc;i++) {

‎0061_rotate_list/rotate_list.cc‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<bits/stdc++.h>
2+
3+
usingnamespacestd;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* struct ListNode {
8+
* int val;
9+
* ListNode *next;
10+
* ListNode() : val(0), next(nullptr) {}
11+
* ListNode(int x) : val(x), next(nullptr) {}
12+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
13+
* };
14+
*/
15+
classSolution {
16+
public:
17+
ListNode*rotateRight(ListNode* head,int k) {
18+
if (head ==nullptr) {
19+
return head;
20+
}
21+
22+
int len =0;
23+
ListNode dummy;
24+
dummy.next = head;
25+
ListNode *tail = &dummy;
26+
while (tail->next !=nullptr) {
27+
len++;
28+
tail = tail->next;
29+
}
30+
31+
ListNode *prev = &dummy;
32+
ListNode *p = head;
33+
k = k % len;
34+
for (int i =0; i < len - k; i++) {
35+
prev = p;
36+
p = p->next;
37+
}
38+
39+
if (p !=nullptr) {
40+
/* deletion*/
41+
prev->next = tail->next;
42+
/* insertion*/
43+
tail->next = head;
44+
head = p;
45+
}
46+
return head;
47+
}
48+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp