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

Example:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 
class Solution {     public ListNode rotateRight(ListNode head, int k) {         if (head == null || head.next == null)              return head;         ListNode dummy=new ListNode(0);         dummy.next = head;         ListNode fast = dummy;         ListNode slow = dummy;                  int i;         for (i = 0; fast.next != null; i++)//让fast指向最后节点,i变成节点个数,此例子中i为5             fast = fast.next;                   for (int j = i - k % i; j > 0; j--)//j等于3,slow往前移动3次,移到3             slow = slow.next;                  fast.next = dummy.next; //使得5指向1         dummy.next = slow.next; //0指向4         slow.next = null; //3指向null                  return dummy.next;             } }
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄