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 ...
随机推荐
- iOS 之 判断是否是第一次打开app
/** App判断第一次启动的方法 */ NSString *key = @"isFirst"; BOOL isFirst = [[NSUserDefaults standardU ...
- 【CF1073A】Diverse Substring(签到)
题意:给定一个由小写字母组成的串,要求找出一个子串使得其中出现最多的字母不超过它长度的一半下取整 n<=1e3 思路: #include<cstdio> #include<cs ...
- java网络编程学习笔记(二):socket详解
1.Socket有多种构造方法,大多数构造方法在构造的时候就指定了连接的主机和端口号.当客户端的构造方法与服务器连接的时候,可能需要等待一段时间,因为需要建立连接.默认情况下,Socket的构造方法会 ...
- Linux STP介绍
1. 介绍 STP(Spanning Tree Protocol)即生成树协议,标准为IEEE802.1D-1998STP是一种二层冗余技术,利用STA算法构建一个逻辑上没有环路的树形网络拓扑结构,并 ...
- HDU5006 Resistance(高斯消元)
给你一个复杂的网路图,然后告诉你s,t,求s,t的等效电阻.方法是设s的电势为1,t的电势为0.然后对于其它的每个点x,满足的是sigma(ux-uy)/R(x,y)(即对每个与x相连的节点y,电势差 ...
- python对象的复制问题
list 的拷贝问题: 1, >>> a [1, 2] >>> b=a[:] >>> b [1, 2] >>> b[0]=20 ...
- 正确地使用GIT FORK
摘自github官方网站,稍后我将抽空翻译. Fork a repo https://help.github.com/articles/fork-a-repo/ Syncing a fork http ...
- 洛谷——P1119 灾后重建
P1119 灾后重建 题目背景 B地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响.但是在村庄重建好之前,所有与未重建完成的村庄的公路均无法通车.换句话说,只有连接着两个重 ...
- slam学习足迹
1.slam入门介绍 2.齐次坐标系 3.贝叶斯滤波 均值:平均值 标准差:样本的集中程度/分散度 方差:标准差的平方 协方差:不同维度之间的关系(相关度) 协方差矩阵:多维度之间的关系(相关度) 4 ...
- hdu4493(C++)
//卡格式的题目 #include<iostream> #include<iomanip>using namespace std;int main(){ int T,i; do ...