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多线程:『GCD』详尽总结 ---(转)
文章:https://bujige.net/blog/iOS-Complete-learning-GCD.html 文中 Demo 我已放在了 Github 上,Demo 链接:https://git ...
- Codeforces 460D. Little Victor and Set
D. Little Victor and Set time limit per test:1 second memory limit per test:256 megabytes input:stan ...
- codechef AUG17 T5 Chef And Fibonacci Array
Chef has an array A = (A1, A2, ..., AN), which has N integers in it initially. Chef found that for i ...
- event.srcElement就是指向触发事件的元素,他是什么就有什么的属性
原文发布时间为:2009-06-29 -- 来源于本人的百度文章 [由搬家工具导入] 得到或设置触发事件的对象。 event.srcElement就是指向触发事件的元素,他是什么就有什么的属性 s ...
- 使用中科大源下载android源码
我的系统时manjaro linux 最新版的,安装过了git和curl软件 一.如果没有安装的同学,请使用命令: pacman -S git curl 我这里安装了python3和python2,但 ...
- [MySQL] xtrabakcup原理
Xtrabackup InnoDB内部的Redo log, 也叫Transaction log file. 存储每一个InnoDB表纪录的修改日志. 当InnoDB启动时, InnoDB会检查数据文件 ...
- C#图解教程学习笔记——事件
一.事件的定义事件:当一个特定的程序事件发生时,程序的其他部分可以得到该事件已经发生的通知,同时运行相应处理程序.事件的很多部分都与委托类似.实际上,事件就像专门用于特殊用途的简单委托.事件包含了一个 ...
- LeetCode OJ-- Count and Say
https://oj.leetcode.com/problems/count-and-say/ 求经过n次变换后,变成了什么. 1 11 21 1211 111221 ps. 3 变成 ‘3 ...
- IIS 发布双证书
1.端口都用443 2.配置主机名 3.勾选需要服务器名称指示
- P1450 包裹快递 RP+14【二分】
[题目链接]:https://vijos.org/p/category/%E5%85%B6%E4%BB%96,%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE 描述 一个快递公 ...