zoukankan      html  css  js  c++  java
  • Leetcode 61

    /**
     * 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) { int num = cnt(head); if(num == 0) return head; k = k%num; if(k == 0) return head; int m = num-k; ListNode* move = head; for(int i=0;i < m-1;i++){ move = move->next; } ListNode* reshead = move->next; ListNode* node1 = reshead; while(node1->next != NULL){node1 = node1->next;} move->next = NULL; node1->next = head; return reshead; } int cnt(ListNode* head){ int res = 0; ListNode* temp = head; while(temp != NULL){ res++; temp = temp->next; } return res; } };
  • 相关阅读:
    HTML初体验
    out传值
    函数
    冒泡排序
    数组
    异常语句
    类 string math
    for 穷举 迭代
    HTML JavaScript及运算符
    HTML 格式与布局
  • 原文地址:https://www.cnblogs.com/cunyusup/p/9760824.html
Copyright © 2011-2022 走看看