Rotate List
2014-02-20 13:09
204 查看
/** * 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 || k<0) return 0; int length=0; ListNode *pCur=head; ListNode *pTail=0; while(pCur) { length++; if(!pCur->next) { pTail=pCur; } pCur=pCur->next; } pTail->next=head; int index=length-k%length; pCur=head; for(int i=1;i<index;i++) { pCur=pCur->next; } head=pCur->next; pCur->next=0; return head; } };
相关文章推荐
- [C/C++]反转链表
- Linux内核链表实现过程
- 找出链表倒数第n个节点元素的二个方法
- Java数据结构之简单链表的定义与实现方法示例
- STL list链表的用法详细解析
- 编码实现从无序链表中移除重复项(C和JAVA实例)
- 结构之美:单链表的初始化、创建与遍历
- 结构之美:单链表的头结点与头指针
- 带头结点与不带头结点的单链表初始化
- 带头结点的单链表的12个基本操作
- 单链表的基础知识问与答
- 结构之美:使用头插法创建单链表
- 结构之美:单链表的销毁删除
- 怎样才算是掌握单链表呢?
- 结构之美:查找单链表指定位置结点的数据
- 结构之美:删除单链表指定位置的数据
- 结构之美:单链表逆序
- 结构之美:判断单链表中是否有环
- 结构之美:获取单链表倒数第N个结点值
- 腾讯面试题:快速找到未知长度单链表的中间节点