LeetCoce-61-Rotate List(链表/指针操作)-Medium
来源:互联网 发布:幼儿园美工课图片 编辑:程序博客网 时间:2024/05/15 01:54
题目理解:
假设先前的链表长度为n,求解将前n-k个元素移动到从k开始的链表之后的结果;
题目分析:
1. 注意k>n的情况(k=k%n);
2. 典型的指针操作;
解题代码:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode rotateRight(ListNode head, int k) { ListNode ans=null; if(head==null){ return ans; } int len=0; ListNode p1=head.next; while(true){ len++; if(p1==null){ break; }else{ p1=p1.next; } } int moveK; if(k<len){ moveK=k; }else{ moveK=k%len; } if(moveK==0){ return head; } p1=head; ListNode p2=p1.next; for(int i=1; i<len-moveK;i++){ p1=p1.next; p2=p1.next; } p1.next=null; ans=p2; p1=p2; while(p1.next!=null){ p1=p1.next; } p1.next=head; return ans; }}
0 0
- LeetCoce-61-Rotate List(链表/指针操作)-Medium
- [Leetcode 61, Medium] Rotate List
- 61. Rotate List(Linked List-Medium)
- Leetcode 61. Rotate List (Medium) (cpp)
- LeetCoce-71-Simplify Path(栈)-Medium
- LeetCode刷题(C++)——Rotate List(Medium)
- LeetCode 61: Rotate List
- [Leecode 61] Rotate List
- [leetcode 61] Rotate List
- [leetcode #61]Rotate List
- 61Rotate List
- leetcode || 61、Rotate List
- LeeCode 61 Rotate List
- 61 Rotate List
- leetcode[61]:Rotate List
- LeetCode 61: Rotate List
- LeetCode(61) Rotate List
- LeetCode 61 Rotate List
- 通过java反射机制获取该类的所有属性类型、值。(
- 读完这个我懂了JNDI
- n皇后问题
- 【OpenSource】【Code】Fragment+Xutils+百度推送
- Kafka+Storm+HDFS整合实践
- LeetCoce-61-Rotate List(链表/指针操作)-Medium
- FZU 1408 位图(宽度优先搜索)
- codeforces Good Bye 2015 D. New Year and Ancient Prophecy
- android 6.0 api23 系统变化[转]
- Redis与Memcached的区别
- Shell基础(一):入门基础
- 124.View the Exhibit and examine the structure of the PROMOTIONS, SALES, and CUSTOMER tables.
- 微赞平台拼团插件的支付流程
- 2015AC前端大会笔记