public class RotateList
{
public RotateList(){}
public ListNode rotateRight(ListNode head, int k)
{
if(head == null || k == 0)
{return head;}
ListNode temp= head;
ListNode temp_1= null;
int num= 0;
while(temp!=null)
{
num++;
temp_1=temp;
temp= temp.next;
}
k= k % num;
if(k == 0)
{return head;}
ListNode result= head;
for(int i=0;i<num-k-1;i++)
{head= head.next;}
temp= head.next;
temp_1.next= result;
result= temp;
head.next= null;
return result;
}
}
最后
以上就是傻傻御姐最近收集整理的关于给定一个链表,将链表向右旋转k个位置,其中k是非负的。的全部内容,更多相关给定一个链表,将链表向右旋转k个位置,其中k是非负内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复