[leetcode-61]Rotate List(c)
来源:互联网 发布:linux解压t命令gunzip 编辑:程序博客网 时间:2024/06/08 17:50
问题描述:
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为轴进行旋转,其中k是剩余的元素,也即,待旋转点距离最后一个点位置恰好为k(其实是k%len(list))。
代码如下:4ms
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* rotateRight(struct ListNode* head, int k) { int length = 0; struct ListNode *headhead = head; while(headhead){ length++; headhead = headhead->next; } if(!length) return head; int index = k%length; if(index==0) return head; struct ListNode *headheadhead = head; headhead = head; while(index--){ headheadhead = headheadhead->next; } while(headheadhead->next!=NULL){ headhead = headhead->next; headheadhead = headheadhead->next; } struct ListNode *tmpHead = headhead->next; headhead->next = NULL; headheadhead->next = head; head = tmpHead; return head;}
0 0
- [leetcode-61]Rotate List(c)
- [C语言][LeetCode][61]Rotate List
- LeetCode 61: Rotate List
- [leetcode 61] Rotate List
- [leetcode #61]Rotate List
- leetcode || 61、Rotate List
- leetcode[61]:Rotate List
- LeetCode 61: Rotate List
- LeetCode(61) Rotate List
- LeetCode 61 Rotate List
- leetcode 61: Rotate List
- Leetcode#61||Rotate List
- [Leetcode]#61 Rotate List
- leetcode 61:Rotate List
- 【leetcode】【61】Rotate List
- 【LeetCode-61】Rotate List
- LeetCode 61 - Rotate List
- ( Leetcode 61 ) Rotate List
- JavaScript之复选框的全选,全不选
- 区间dp一般模板
- 设计模式 外观模式
- javascript制作计时器
- aidl 个人心得
- [leetcode-61]Rotate List(c)
- squid之安装配置运行
- 根据多年经验整理的《互联网MySQL开发规范》
- 本文示例如何使用SAP FTP Function将文件从应用服务器传输到另外一个FTP服务器上。
- jQuery模拟抛物线轨迹动画
- Java数组与泛型
- Ubuntu 命令配置
- slickedit字体设置——使用Source Code Pro字体
- iOS优化相关---加速Table Views开发的10个建议