Rotate List leetcode java

题目:

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.

题解:

这道题主要先理解题意,就是倒着数k个node,从那开始到结尾和之前那部分对调,那个例子就是,4->5拿前面来,1->2->3拿后面去。

几个特殊:

k是可以大于整个list的长度的,所以这时要对k对len取模

如果取模之后得0,相当于不用rotate,直接返回

处理完特殊情况后,就用熟悉的faster/slower双指针解决就好(看到这种linkedlist,倒着数数的,就条件反射了)

先对faster设步长为n,然后faster和slower再一起走,知道faster.next==null,说明slower指向要倒着数的开始点的前一个位置。

所以slow.next就是要返回的newhead,保存一下。

然后把faster.next接到head上,slower.next存为null,作队尾。

这样就把list给rotate了。

这是我想的一种解法,还有一种就是把整个list连起来,变成环,找到切分点断开就行。

解法1:

1    public ListNode rotateRight(ListNode head, int n) {
 2         if(head==null||head.next==null||n==0)
 3             return head;
 4         ListNode fast = head, slow = head,countlen = head;
 5         ListNode newhead = new ListNode(-1);
 6         int len = 0;
 7         
 8         while(countlen!=null){
 9             len++;
10             countlen = countlen.next;
11         }
12         
13         n = n%len;
14         if(n==0)
15             return head;
16         
17         for(int i = 0; i < n; i++)
18             fast = fast.next;
19         
20         while(fast.next!=null){
21             slow = slow.next;
22             fast = fast.next;
23         }
24         
25         newhead = slow.next;
26         fast.next = head;
27         slow.next = null;
28         
29         return newhead;
30     }

解法2:

1 public ListNode rotateRight(ListNode head, int n) {
 2 
 3     if (head == null || n == 0)
 4         return head;
 5     ListNode p = head;
 6     int len = 1;//since p is already point to head
 7     while (p.next != null) {
 8         len++;
 9         p = p.next;
10     }
11     p.next = head; //form a loop
12     n = n % len;
13     for (int i = 0; i < len - n; i++) { 
14         p = p.next;
15     } //now p points to the prev of the new head
16     head = p.next;
17     p.next = null;
18     return head;
19 }

Reference for 2: http://leetcodenotes.wordpress.com/2013/08/14/leetcode-rotate-list-%E6%8A%8A%E5%90%8Ek%E4%B8%AArotate%E5%88%B0list%E5%89%8D%E9%9D%A2%E5%8E%BB%EF%BC%8Ck%E5%8F%AF%E4%BB%A5%E8%B6%85%E8%BF%87list%E6%9C%AC%E8%BA%AB%E9%95%BF%E5%BA%A6/

Rotate List leetcode java,布布扣,bubuko.com

时间: 2024-10-11 12:15:44

Rotate List leetcode java的相关文章

Rotate Image leetcode java

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 题解: 这道题就是考察很直白的旋转坐标.要in place的.画个图自己算算就出来了. 代码如下: 1  /*   public void rotate(int[][] matrix) { 2         in

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的

Triangle leetcode java

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Word Ladder leetcode java

题目: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: