Problem Description :
Given a linked list, rotate the linked list and move each node of the linked list k positions to the right, where k is a non-negative number.
Example 1:
Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
Rotate right 1 step: 5->1->2->3->4->NULL
Rotate right 2 steps: 4->5->1->2->3->NULL
Example 2:
Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
Rotate 1 step to the right: 2->0->1->NULL
Rotate right 2 steps: 1->2->0->NULL
Rotate right 3 steps: 0->1->2->NULL
Rotate right 4 steps: 2->0->1->NULL
The following code can be used to complete the rotateRight function, where the formal parameter head points to the singly linked list of headless nodes, and k is the number of rotation steps, and returns the pointer of the linked list head after rotation.
#include
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(NULL) {}
ListNode(int x) : val(x), next(NULL) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
//完成本函数
}
};
ListNode *createByTail()
{
ListNode *head;
ListNode *p1,*p2;
int n=0,num;
int len;
cin>>len;
head=NULL;
while(n<len && cin>>num)
{
p1=new ListNode(num);
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
}
return head;
}
void displayLink(ListNode *head)
{
ListNode *p;
p=head;
cout<<"head-->";
while(p!= NULL)
{
cout<<p->val<<"-->";
p=p->next;
}
cout<<"tail\n";
}
int main ()
{
int k;
ListNode* head = createByTail();
cin>>k;
head=Solution().rotateRight(head,k);
displayLink(head);
return 0;
}
Input description:
First enter the length of the linked list len, and then enter len integers, separated by spaces.
Then enter the non-negative integer k.
Output description:
See example for output format
Input example:
5
1 2 3 4 5
2
Output example:
head–>4–>5–>1–>2–>3–>tail
算法思想:
先将链表成环,然后找到新的链表尾,链表头,新的链表头在n-k处,那么链表尾在n-k-1处
当k>=n时,链表尾就是n-k%n-1
*/
#include<iostream>
#include<vector>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(NULL) {
}
ListNode(int x) : val(x), next(NULL) {
}
ListNode(int x, ListNode *next) : val(x), next(next) {
}
};
class Solution
{
public:
ListNode* rotateRight(ListNode* head, int k) {
if(head == NULL)
{
return NULL; //空链表
}
if(head->next == NULL)
{
return head; // 只有一个节点
}
//先将链表成环
ListNode *p = head;
int len = 1;
while(p->next)
{
len++;
p = p->next;
}
p->next = head;
//找到新的链表尾断链
ListNode *new_tail = head;
for(int i = 0; i < len - k%len -1; i++)
{
new_tail = new_tail->next;
}
ListNode *new_head = new_tail->next; //新的头节点
new_tail->next = NULL; //尾节点后继为NULL
return new_head;
}
};
ListNode *createByTail()
{
ListNode *head;
ListNode *p1,*p2;
int n=0,num;
int len;
cin>>len;
head=NULL;
while(n<len && cin>>num)
{
p1=new ListNode(num);
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
}
return head;
}
void displayLink(ListNode *head)
{
ListNode *p;
p=head;
cout<<"head-->";
while(p!= NULL)
{
cout<<p->val<<"-->";
p=p->next;
}
cout<<"tail\n";
}
int main()
{
int k;
ListNode* head = createByTail();
cin>>k;
head=Solution().rotateRight(head,k);
displayLink(head);
return 0;
}