LeetCode 题解(37): Rotate List

来源:互联网 发布:asp.net php 哪个好 编辑:程序博客网 时间:2024/05/22 09:05

题目:

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

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

题解:

将指针移动到链表尾部,并计算出链表长度length。将链表尾首相连,再将指针移动length-k%length步到达新链表的尾部,设置新的头指针,并将尾首相断开。返回新的头指针。

class Solution {public:    ListNode *rotateRight(ListNode *head, int k) {        if(!head || k <= 0 || !head->next)            return head;                ListNode* tail = head;         int length = 1;        while(tail->next)        {            length =length+1;            tail = tail->next;        }        tail->next = head;        int count = length - k % length;        while(count)        {            tail = tail->next;            count--;        }        head = tail->next;        tail->next = NULL;                return head;    }};


2015年2月3日重写,发现以前写的比现在还简洁,吐了。不过运行时间由之前的56ms提升到16ms。

class Solution {public:    ListNode *rotateRight(ListNode *head, int k) {        if(!head)            return NULL;                if(k < 0)            return head;                    ListNode *temp = head;        ListNode *tail = NULL;        int length = 0;        while(temp != NULL) {            length++;            if(temp->next == NULL)                tail = temp;            temp = temp->next;        }                if(k % length == 0)            return head;                return rotateN(head, k % length, length, tail);    }        ListNode *rotateN(ListNode *head, int n, int length, ListNode *tail) {        ListNode *pre_head = head;                for(int i = 1; i < length - n; i++)            head = head->next;                    ListNode* cur_tail = head;        head = head->next;        tail->next = pre_head;        cur_tail->next = NULL;                return head;    }};

Java版:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode rotateRight(ListNode head, int n) {        if(head == null)            return null;                if(n < 0)            return head;                    int length = 0;        ListNode temp = head;        ListNode tail = head;        while(temp != null) {            length += 1;            if(temp.next == null)                tail = temp;            temp = temp.next;        }                if(n % length == 0)            return head;                    ListNode pre_head = head;        for(int i = 1; i < length - (n % length); i++)            head = head.next;                    ListNode new_tail = head;        head = head.next;                tail.next = pre_head;        new_tail.next = null;                return head;    }}

Python版:

# Definition for singly-linked list.# class ListNode:#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution:    # @param head, a ListNode    # @param k, an integer    # @return a ListNode    def rotateRight(self, head, k):        if head == None:            return None                if k < 0:            return head                    length = 0        tail = None        temp = head                while temp != None:            length += 1            if temp.next == None:                tail = temp            temp = temp.next                    if k % length == 0:            return head                    pre_head = head        for i in range(1,length-(k%length)):            head = head.next                    new_tail = head        head = head.next                tail.next = pre_head        new_tail.next = None                return head


0 0