zoukankan      html  css  js  c++  java
  • 链表代码Rotate List

    文章结束给大家来个程序员笑话:[M]

        题目:

        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.

        分析:先求出链表长度n,找到第n-k个结点,停止旋转。

        代码如下:

          struct ListNode

          {

          int val;

           ListNode *next;

           ListNode(int x) : val(x), next(NULL) {}

           };

        每日一道理
    “上下五千年,龙的看火不灭;古有愚公志,而今从头越…… ”站在新世纪的门槛上,我们的追求就是让祖国灿烂的喜悦飞扬在美好的明天……

            ListNode *rotateRight(ListNode *head, int k) {
            if(head==NULL)return NULL;
            ListNode * head1=head;
            int i=1,j=1;
            while(head1->next!=NULL)
            {
                i++;
                head1=head1->next;
            }
            k=k%i;
            if(k==0||k==i)return head;
            ListNode * head2=head;
            while(head2->next!=NULL&&(i-k)!=j)
            {
                j++;
                head2=head2->next;
            }
            head1->next=head;
            head=head2->next;
            head2->next=NULL;
            return head;
        }

    文章结束给大家分享下程序员的一些笑话语录: 关于编程语言
    如果 C++是一把锤子的话,那么编程就会变成大手指头。
    如果你找了一百万只猴子来敲打一百万个键盘,那么会有一只猴子会敲出一 段 Java 程序,而其余的只会敲出 Perl 程序。
    一阵急促的敲门声,“谁啊!”,过了 5 分钟,门外传来“Java”。
    如果说 Java 很不错是因为它可以运行在所有的操作系统上,那么就可以说 肛交很不错,因为其可以使用于所有的性别上。

  • 相关阅读:
    链接
    列表
    Android Studio AVD 虚拟机 联网 失败
    docker error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuratio
    JSP Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING
    js jsp form
    intellij jsp 中文乱码
    [转载]在Intellij Idea中使用JSTL标签库
    windows pybloomfilter
    docker mysql
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3086094.html
Copyright © 2011-2022 走看看