LeetCode 刷题记录61. Rotate List

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

Example 1:

Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL
Example 2:

Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
解法1:
快慢指针法
首先k的长度可以大于链表的长度,所以首先要遍历一遍列表获取链表的长度n,然后k对n取余
然后进行快慢指针法,快慢指针指的是一遍确定倒数第k个节点
如k=2,即确定4的位置
首先快慢指针都处在head的位置,快指针先走k步,慢指针不走
然后快慢指针一起走,等快指针走到最后一个节点时,慢指针即到了目标节点的前一个结点,如在本题中就是3
这里有个特殊情况:如果倒数第k个节点就是head节点,则慢指针怎么也走不到目标节点的前一个节点
两种处理方式:

  1. 在头节点前再加一个节点dummy
  2. 对这种情况进行特判:
 if(fast == nullptr) return head;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
c++:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head == nullptr || k == 0) return head;
        ListNode* cur = head;
        int n = 0;
        while(cur){
            n++;
            cur = cur->next;
        }
        k %= n;
        ListNode* fast = head;
        ListNode* slow = head;
        while(k--){
            fast = fast->next;
        }
        if(fast == nullptr) return head;
        while(fast->next){
            fast = fast->next;
            slow = slow->next;
        }
        fast->next = head;
        fast = slow->next;
        slow->next = nullptr;
        return fast;
    }
};

java:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(head == null || k == 0) return head;
        ListNode cur = head;
        int n = 0;
        while(cur != null){
            n++;
            cur = cur.next;
        }
        k %= n;
        ListNode fast = head;
        ListNode slow = head;
        while(k-- != 0){
            fast = fast.next;
        }
        if(fast == null) return head;
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        fast.next = head;
        fast = slow.next;
        slow.next = null;
        return fast;
    }
}

python:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if head is None or k == 0:
            return head
        
        cur = head
        n = 0
        while cur != None:
            n += 1
            cur = cur.next
        
        k %= n
        fast = slow = head
        for i in range(k):
            fast = fast.next
        
        if fast is None: return head
        while fast.next != None:
            fast = fast.next
            slow = slow.next
        
        
        fast.next = head
        fast = slow.next
        slow.next = None
        return fast

解法2:
由于之前已经遍历过链表过一遍,所以用快慢指针就没有意义,我们可以用一个指针就能确定倒数第k个节点的位置
走的步数为

int m = n - k%n;

c++:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head == nullptr || k == 0) return head;
        ListNode* cur = head;
        int n = 1;
        while(cur->next){
            n++;
            cur = cur->next;
        }
        int m = n - k%n;
        cur->next = head;
        while(m--){
            cur = cur->next;
        }
       
        ListNode* newhead = cur->next;
        cur->next = nullptr;
        return newhead;
    }
};
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(head == null || k == 0) return head;
        ListNode cur = head;
        int n = 1;
        while(cur.next != null){
            n++;
            cur = cur.next;
        }
        int m = n - k%n;
        cur.next = head;
        while(m-- != 0){
            cur = cur.next;
        }
       
        ListNode newhead = cur.next;
        cur.next = null;
        return newhead;
         
    }
}
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if head is None or k == 0:
            return head
        
        cur = head
        n = 1
        while cur.next != None:
            n += 1
            cur = cur.next
        
        m = n - k % n
        cur.next = head
        for i in range(m):
            cur = cur.next
        
        
        newhead = cur.next
        cur.next = None
        return newhead

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值