我是靠谱客的博主 傻傻御姐,这篇文章主要介绍给定一个链表,将链表向右旋转k个位置,其中k是非负的。,现在分享给大家,希望可以做个参考。

public class RotateList
{
public RotateList(){}

复制代码
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
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是非负内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部