LeetCode进阶之路(Rotate List)

原创 2016年08月28日 21:57:24

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位置,第一位,然后把三段拼起来。思路是正确,但是太复杂,绕来绕去,后面提交也是超时了。

public ListNode rotateRight(ListNode head, int k) {
		
		ListNode result = new ListNode(-1);
		result.next = head;
		ListNode first = result;
		ListNode second = head;
		ListNode third = result;
		if(head == null) {
			return result.next;
		}
		if(head.next == null){
			return head;
		}
		int len = 1;
		while(result.next != null) {
			len++;
			result = result.next;
		}
		int i = 0;
		while(i < len-k) {
			i++;
			first = first.next;
		}
		third.next = first;
		int j = 1;
		while(j < len-1-k) {
			second = second.next;
			j++;
		}
		second.next = null;
		
		ListNode flag = third;
		while(flag.next != null) {
			flag = flag.next;
		}
		flag.next = head;
		
		return third.next;
		
		
	}

下面是参考网友的思路,用两个指针,faster/slower,先对faster设步长为n,然后faster和slower再一起走,知道faster.next==null,说明slower指向要倒着数的开始点的前一个位置。

public ListNode rotateRight(ListNode head, int n) {
         if(head==null||head.next==null||n==0)
            return head;
        ListNode fast = head, slow = head,countlen = head;
        ListNode newhead = new ListNode(-1);
        int len = 0;
        
        while(countlen!=null){
            len++;
            countlen = countlen.next;
        }
        
        n = n%len;
        if(n==0)
            return head;
        
        for(int i = 0; i < n; i++)
            fast = fast.next;
        
        while(fast.next!=null){
            slow = slow.next;
            fast = fast.next;
        }
        
        newhead = slow.next;
        fast.next = head;
        slow.next = null;
        
        return newhead;
    }
还有一种更简单的思路,把整个链表连成一个环,在重新分割就可以了。

public ListNode rotateRight(ListNode head, int n) {

    if (head == null || n == 0)
        return head;
    ListNode p = head;
    int len = 1;//since p is already point to head
    while (p.next != null) {
        len++;
        p = p.next;
    }
    p.next = head; //form a loop
    n = n % len;
    for (int i = 0; i < len - n; i++) { 
        p = p.next;
    } //now p points to the prev of the new head
    head = p.next;
    p.next = null;
    return head;
}

种一棵树最好的时间是十年前,其次是现在!



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode之路048 Rotate Image

题目大意:给定一个代表图片的n*n的2d矩阵,将它顺时针旋转90,并且满足就地操作。 思路: 思路1、自己想的思路,比较复杂:对于一个点(x,y),令i=x,j=y,交换(x,y)和(j,n-i-...

leetCode61:Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given...

leetcode解题61. Rotate List java版(旋转链表)

leetcode解题61. Rotate List java版(旋转链表)

(java)leetcode-61:Rotate List

Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For...

leetcode | Rotate List

Rotate List :https://leetcode.com/problems/rotate-list/问题描述Given a list, rotate the list to the righ...

Leetcode 61. Rotate List (Medium) (cpp)

Leetcode 61. Rotate List (Medium) (cpp)

[LeetCode]61.Rotate List

【题目】 Given a list, rotate the list to the right by k places, where k is non-negative. For example...

【LeetCode】61. Rotate List

【LeetCode】61. Rotate List 快慢指针的应用

LeetCode 61. Rotate List(Python详解及实现)

【题目】 Given a list, rotate the list to the rightby k places, where k is non-negative.   For exampl...

【LeetCode】 Rotate List 循环链表

题目:rotate list 解法1: /**LeetCode Rotate List:Given a list, rotate the list to the right by k places...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)