leetCode 61.Rotate List (旋转链表) 解题思路和方法
Rotate List
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.
思路:题目非常清晰。思路是先得到链表长度。再从头開始直到特定点,開始变换连接就可以。
代码例如以下:
/**
* 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) {
if(k == 0 || head == null)
return head;
int len = 0;
ListNode first = head;//头结点
ListNode last = null;//尾节点
while(head != null){
len++;//求长度
last = head;//最后一个节点
head = head.next;//循环条件
} k = k%len;//假设k>len,取余数
int n = 0;
head = first;//标记到头结点
while(head!= null){
if(++n == (len - k))//推断是否到达位置
break;
head = head.next;
}
//下面为交换位置
last.next = first;
first = head.next;
head.next = null;
return first;
}
}/**
* 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) {
if(k == 0 || head == null)
return head;
int len = 0;
ListNode first = head;//头结点
ListNode last = null;//尾节点
while(head != null){
len++;//求长度
last = head;//最后一个节点
head = head.next;//循环条件
} k = k%len;//假设k>len,取余数
int n = 0;
head = first;//标记到头结点
while(head!= null){
if(++n == (len - k))//推断是否到达位置
break;
head = head.next;
}
//下面为交换位置
last.next = first;
first = head.next;
head.next = null;
return first;
}
}
leetCode 61.Rotate List (旋转链表) 解题思路和方法的更多相关文章
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- [leetcode]61. Rotate List旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- leetCode 86.Partition List(分区链表) 解题思路和方法
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [leetcode]61. Rotate List反转链表k个节点
类似于找链表的后k个节点 不同的是要把前边的接到后边 public ListNode rotateRight(ListNode head, int k) { //特殊情况 if (head==null ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
随机推荐
- Gerrit使用简介
Gerrit,一种免费.开放源代码的代码审查软件,使用网页界面. Gerrit,一种免费.开放源代码的代码审查软件,使用网页界面.利用网页浏览器,同一个团队的软件程序员,可以相互审阅彼此修改后的程序代 ...
- nodejs+express+mongodb搭建博客
https://github.com/lanleilin/sayHelloBlog 是可以运行的 https://github.com/lanleilin/sayHelloBlog 文件结构如下: c ...
- cf 126D - Fibonacci Decomposition
题目大意 \(t\le 10^5\)组询问 每次询问\(1\le n\le 10^{18}\) 求有多少种\(n\)的\(fibonacci\)分解 分解定义为:每个\(fib\)数最多选一个,且和为 ...
- Python与其他语言时间戳
时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数.它也被称为 Unix 时间戳(Unix Timestamp). Unix时间戳(Unix timestamp),或称Un ...
- 【源码】List<T>泛型绑定repeater,以及repeater的交替绑定
原文发布时间为:2009-10-28 -- 来源于本人的百度文章 [由搬家工具导入] 后台: using System;using System.Collections.Generic; public ...
- gridview行链接
原文发布时间为:2009-04-21 -- 来源于本人的百度文章 [由搬家工具导入] 点击行,链接!! 可这样,在GridView的RowDataBound输入代码,假如id在第0列,且不是摸板列: ...
- 爬虫学习笔记(六)PyQuery模块
PyQuery模块也是一个解析html的一个模块,它和Beautiful Soup用起来差不多,它是jquery实现的,和jquery语法差不多,会用jquery的人用起来就比较方便了. Pyquer ...
- Codeforces 922F Divisibility (构造 + 数论)
题目链接 Divisibility 题意 给定$n$和$k$,构造一个集合$\left\{1, 2, 3, ..., n \right\}$的子集,使得在这个集合中恰好有$k$对正整数$(x, y) ...
- html5扫面二维码逻辑
写在前面 项目中有这样的需求,在android端嵌入的html5应用中,需要扫描二维码,而一般的浏览器是不允许你调用摄像头的.最后时限方式是由app的webview进行扫描,将扫描结果返回,也就是js ...
- Auto-Test 要点纪录(一)
1,select下拉框类型 使用工具可以看到html对应标签为<select>这类标签才是真正的下拉框类型就需要对应的方法,不能但看页面上的效果,有的做成了效果但其实不是select类型即 ...