码疯窝

LeetCode 每日一题 — Rotate List

2015/01/12 11:18:22    分类: 日志连载    0人评论 次浏览

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.

算法分析: 例子很简单, 就是将k 处的节点反转, 所以先求出 list 的长度, 然后再将k 处反转. 但是真正写的时候要注意, 因为test case 里面 k 可能比length 还要长. 所以真正反转的应该是k % len, 当k 为0 时, 就不需要反转.

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) {
        int len = 0;
        int i = 0;
        if (head == null || head.next == null || n == 0)
            return head;
        ListNode tmp = head;
        while (tmp != null) {
            len++;
            tmp = tmp.next;
        }
        n = n % len;
        if (n == 0)
            return head;
        ListNode rotate = null;
        tmp = head;
        while(i < (len - n - 1)) {
            tmp = tmp.next;
            i++;
        }
        rotate = tmp.next;
        tmp.next = null;
        ListNode newlist = rotate;
        
        while (newlist.next != null) {
            newlist = newlist.next;
        }
        newlist.next = head;
        return rotate;
    }
}

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 == NULL || head->next == NULL || k == 0)
            return head;
        int i = 0;
        int len = 0;
        ListNode *tmp = head;
        while (tmp != NULL) {
            len++;
            tmp = tmp->next;
        }
        k = k % len;
        if (k == 0)
            return head;
        tmp = head;
        ListNode *rotate = head;
        while (i < len - k - 1) {
            tmp = tmp->next;
            i++;
        }
        rotate = tmp->next;
        tmp->next = NULL;
        ListNode *newlist = rotate;
        while(newlist->next != NULL) {
            newlist = newlist->next;
        }
        newlist->next = head;
        return rotate;
        
    }
};

Python:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # @param head, a ListNode
    # @param k, an integer
    # @return a ListNode
    def rotateRight(self, head, k):
        len = 0
        if (head == None or head.next == None  or k == 0):
            return head
        tmp = head
        while(tmp != None):
            len += 1
            tmp = tmp.next
        k = k % len
        if (k == 0):
            return head
        rotate = head
        tmp = head
        for i in range(0, len - k - 1):
            tmp = tmp.next
        rotate = tmp.next
        tmp.next = None
        newlist = rotate
        while(newlist.next != None):
            newlist = newlist.next
        newlist.next = head
        return rotate
继续查看有关 日志连载的文章

0个访客评论