旋转链表

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。

  示例 1:
    输入: 1->2->3->4->5->NULL, k = 2
    输出: 4->5->1->2->3->NULL
    解释:
    向右旋转 1 步: 5->1->2->3->4->NULL
    向右旋转 2 步: 4->5->1->2->3->NULL
  示例 2:
    输入: 0->1->2->NULL, k = 4
    输出: 2->0->1->NULL
    解释:
    向右旋转 1 步: 2->0->1->NULL
    向右旋转 2 步: 1->2->0->NULL
    向右旋转 3 步: 0->1->2->NULL
    向右旋转 4 步: 2->0->1->NULL

思路:1.反转整个链表

  2.反转前k个链表

  3.反转k结点后面的链表

代码如下:

public ListNode rotateRight(ListNode head, int k) {
        if(head == null || head.next == null) return head;
        //统计链表长度
        ListNode c = head;
        int count = 0;
        while(c!=null){
            c = c.next;
            count++;
        }
        //翻转次数
        int t = k%count;
        if(t == 0) return head;
        //逆转链表
        ListNode cur = reverse(head);
        //定义临时结点sign,找到第t个结点
        ListNode sign = cur;
        while(t>1){
            sign = sign.next;
            t--;
        }
        //让p指向第t个结点的后一个结点
        //让第t个结点的下一个结点指向空
        ListNode p = sign.next;
        sign.next = null;
        //逆转前k个结点
        ListNode m = reverse(cur);
        //n指向反转链表的头结点
        ListNode n = m;
        //找到逆转后第k个结点
        while(n.next!=null){
            n= n.next;
        }
        //让第k个结点的下一个结点指向  后面逆转链表的头指针
        n.next = reverse(p);
        return m;
    }
    //链表逆序
    public static ListNode reverse(ListNode head){
        if(head == null || head.next == null) return head;
        ListNode newHead = reverse(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
原文地址:https://www.cnblogs.com/du001011/p/10640285.html