四种语言刷算法之旋转链表

力扣61. 旋转链表

1、C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* rotateRight(struct ListNode* head, int k){
    if(head==NULL)return head;
    struct ListNode* p = head;
    struct ListNode *rr = NULL;
    int count = 0;
    while(p!=NULL){
        if(p->next==NULL)rr = p;
        count++;
        p = p->next;
    }
    int r_count = k%count;
    if(r_count==0)return head;
    struct ListNode* q = head;
    for(int i=0;i<count-r_count-1;i++){
        q = q->next;
    }
    struct ListNode* r = q->next;
    q->next = NULL;
    rr->next = head;
    return r;
}

2、C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head==nullptr)return head;
        ListNode *p = head;
        ListNode *rr = nullptr;
        int count = 0;
        while(p!=nullptr){
            if(p->next==nullptr){
                rr = p;
            }
            p = p->next;
            count++;
        }
        int r_count = k%count;
        if(r_count==0)return head;
        p = head;
        for(int i=0;i<count-r_count-1;i++){
            p = p->next;
        }
        ListNode *q = p->next;
        p->next=nullptr;
        rr->next = head;
        return q;
    }
};

3、JAVA

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(head==null)return head;
        ListNode p = head;
        ListNode rr = null;
        int count = 0;
        while(p!=null){
            if(p.next==null){
                rr = p;
            }
            p = p.next;
            count++;
        }
        int r_count = k%count;
        if(r_count==0)return head;
        p = head;
        for(int i=0;i<count-r_count-1;i++){
            p = p.next;
        }
        ListNode q = p.next;
        p.next = null;
        rr.next = head;
        return q;
    }
}

4、Python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if head is None:
            return head
        p = head
        rr = None
        count = 0;
        while p is not None:
            count += 1
            if p.next is None:
                rr = p
            p = p.next
        r_count = k%count
        if r_count==0:
            return head
        p = head
        for i in range(count-r_count-1):
            p = p.next
        q = p.next
        p.next = None
        rr.next = head
        return q
posted @ 2023-03-25 16:18  菜鸟冲冲冲  阅读(26)  评论(0)    收藏  举报