Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

  • 定义两个指针,分布指向 list 的头结点和尾结点,
  • 将头结点插到尾部

代码:

class Solution {
public:ListNode* rotateRight(ListNode* head, int k) {ListNode *h1=head;ListNode *h2=head;if(k==0||head==NULL)return h1;int l=1;while(h1&&h1->next){h1=h1->next;l++;}//  if(l>1)int c=l-k%l;// cout<<h1->val<<endl;//  cout<<l<<endl;//   cout<<c<<endl;if(l==1)return head;for(int i=0;i<c;i++){ListNode *t=h2;h2=h2->next;cout<<h2->val<<endl;cout<<t->val<<endl;h1->next=t;h1=t;t->next=NULL;}return h2;}
};

  

转载于:https://www.cnblogs.com/fanhaha/p/7347132.html

leetcode 61. Rotate List相关推荐

  1. leetcode 61 Rotate List ----- java

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  2. LeetCode 61. Rotate List

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...

  3. 【Leetcode -61.旋转链表 -82.删除排序链表中的重复元素Ⅱ】

    Leetcode Leetcode -61.旋转链表 Leetcode -82.删除排序链表中的重复元素Ⅱ Leetcode -61.旋转链表 题目:给你一个链表的头节点 head ,旋转链表,将链表 ...

  4. [勇者闯LeetCode] 189. Rotate Array

    [勇者闯LeetCode] 189. Rotate Array Description Rotate an array of n elements to the right by k steps. F ...

  5. Leetcode Golang 61. Rotate List.go

    思路 先把list变成一个环,然后转到指定位置 注意有个取模的处理 code func rotateRight(head *ListNode, k int) *ListNode {if head == ...

  6. 61. Rotate List

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...

  7. LeetCode 61~65

    前言 本文隶属于专栏<LeetCode 刷题汇总>,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢! 本专栏目录结构请见LeetCode 刷题汇总 正文 幕布 幕 ...

  8. [LeetCode 题解]: Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  9. LeetCode 61. 旋转链表

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

最新文章

  1. pandas遍历dataframe的行:迭代遍历dataframe的数据行、iterrows函数、itertuple函数
  2. window与共享文件夹访问相关的四个服务
  3. extern “C”的作用详解
  4. Iterator和ListIterator接口的使用和区别
  5. Daily Report 2012/11/09 陈伯雄(step 9)
  6. wxWidgets:wxHtmlCellEvent类用法
  7. HTML5 FileReader API 测试(一)
  8. 网络传输数据的加密过程详解
  9. 如何使用pandas正确读取带有中文的cvs文件
  10. Delphi 开发 ERP [1] 准备
  11. 论文用impact、effect、influence 什么区别
  12. ununtu16.04+python3+selenium+firefox环境搭建
  13. linux下编译运行驱动
  14. 与数字化先锋共绘创新蓝图
  15. 紫光输入法终于有新版本了--紫光华宇拼音输入法V5P
  16. 视频编解码器讲解-H.264、VP9、HEVC、AV1
  17. css的white-space属性:normal,nowrap,pre,pre-line和pre-wrap的区别?
  18. 从SDLC到DevSecOps的转变
  19. java gps轨迹_记录 GPS 轨迹 LogGPS
  20. 2022年十大知名堡垒机品牌你真的知道吗?

热门文章

  1. Linux 网卡驱动学习(二)(网络驱动接口小结)
  2. JSP学习之include指令
  3. HDSF主要节点解说(二)工作原理
  4. [Example of Sklearn] - Example
  5. 自动化运维专题之zabbix课程内容大纲
  6. 这可不是一般的便宜哦!
  7. jlpt报名系统显示网络错误和服务器忙,批改网作文提交不了一直显示服务器或网络错误...
  8. springboot整合redisson实现多种分布式锁
  9. OpenCV计算机视觉实战(Python版)_004图像形态学处理
  10. mysql 判断数据是否在_MySQL更新数据之前是否判断数据是否有被修