首页 > 其他分享 >Rotate List

Rotate List

时间:2023-05-02 14:22:20浏览次数:23  
标签:head Rotate ListNode List len next 链表 节点

Source

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

Example
Given 1->2->3->4->5 and k = 2, return 4->5->1->2->3.

题解

旋转链表,链表类问题通常需要找到需要处理节点处的前一个节点。因此我们只需要找到旋转节点和最后一个节点即可。需要注意的细节是 k 有可能比链表长度还要大,此时需要取模,另一个 corner case 则是链表长度和 k 等长。

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param head: the List
     * @param k: rotate to the right k places
     * @return: the list after rotation
     */
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null) return head;
        ListNode fast = head, slow = head;
        int len = 1;
        for (len = 1; fast.next != null && len <= k; len++) {
            fast = fast.next;
        }
        // k mod len if k > len
        if (len <= k) {
            k = k % len;
            fast = head;
            for (int i = 0; i < k; i++) {
                fast = fast.next;
            }
        }
        // forward slow and fast
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        // return new head
        fast.next = head;
        head = slow.next;
        slow.next = null;

        return head;
    }
}

源码分析

由于需要处理的是节点的前一个节点,故最终的while 循环使用 fast.next != null. k 与链表等长时包含在len <= k中。

复杂度分析

时间复杂度 O(n), 空间复杂度 O(1).  

标签:head,Rotate,ListNode,List,len,next,链表,节点
From: https://www.cnblogs.com/lyc94620/p/17367647.html

相关文章

  • 【美化】videoTogther嵌入自己的Alist中并优化
    将下面的代码加到设置->全局->自定义头部里<script>//排除登陆与后台页面if(document.location.pathname.substr(1,6)!="@login"&&document.location.pathname.substr(1,7)!="@manage"){//动态引入videoTogthersetTimeo......
  • MFC-NM_CLICK鼠标左键点击CListCtrl控件消息
    NM_CLICK是鼠标左键点击CListCtrl控件客户区时激发的消息添加消息函数选中控件-->          ......
  • MFC-CListCtrl-获得总列数
     intnHeadNum=mylist4.GetHeaderCtrl()->GetItemCount();//获得总列数str.Format(_T("总列数nHeadNum=%d\r\n"),nHeadNum);OutputDebugString(str);   ......
  • MFC-CListCtrl-DeleteAllItems删除所有项
     BOOLb7=mylist4.DeleteAllItems();//删除所有项    ......
  • 让ListView中有些长按时能弹出contextMenu,有些不能。
    android开发的时候,定义了一个listView,并为他设置了setOnCreateContextMenuListener的监听,但是这样做只能使这个listView中的所有项在长按的时候弹出contextMenu。我希望的是有些长按时能弹出contextMenu,有些不能。解决这个问题的办法是为这个listView设置s......
  • 在ScrollView添加一个ListView造成的滚动问题的简单解决办法
    已不推荐!推荐:http://gundumw100.iteye.com/blog/1732987正常来说,在ScrollView添加一个ListView后在真机上只会显示ListView的一行多一点,我也不理解为什么会这样,后来我把ListView的layout_height改成400dip,而不是用match_parent和wrap_content,我发现这样的话ListView就显示的多了很......
  • 关于如何获得ListView中选中项的值
    我已经获得了手机中保存的电话簿中联系人姓名和电话号码,并把它们显示在了一个ListView中,现在要实现的功能是当点击选中项时直接拨号,那么如何取得此时ListView中的号码?要显示联系人姓名和电话号码,那你现在肯定已经在listview的item里面放了两个控件吧,假如说......
  • LayerDrawable层叠样式layer-list
    layer-list可以将多个图片按照顺序层叠起来。语法:在drawalbe/drawable-layer.xml中<layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@android:color/white"/><itemand......
  • 圆角背景的ListView
    先定义一张圆角的图片shape_bg_listview.xml<?xmlversion="1.0"encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><gradient......
  • Android提高第十五篇之ListView自适应实现表格
    上次介绍了使用GridView实现表格,这次就说说如何用ListView实现自适应的表格。GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(格单元大小不一)。......