LeetCode 61

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.

 1 /*************************************************************************
 2     > File Name: LeetCode061.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: Tue 17 May 2016 18:47:57 PM CST
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Rotate List
11
12     Given a list, rotate the list to the right by k places,
13     where k is non-negative.
14
15     For example:
16     Given 1->2->3->4->5->NULL and k = 2,
17     return 4->5->1->2->3->NULL.
18
19  ************************************************************************/
20
21 #include <stdio.h>
22 /**
23  * Definition for singly-linked list.
24  * struct ListNode {
25  *     int val;
26  *     struct ListNode *next;
27  * };
28  */
29 struct ListNode* rotateRight(struct ListNode* head, int k)
30 {
31     struct ListNode* fast = head;
32     struct ListNode* slow = head;
33     struct ListNode* newhead = head;
34     int length = 1;
35
36     if( head == NULL || k < 0 )
37     {
38         return head;
39     }
40
41     while( fast->next != NULL )
42     {
43         fast = fast->next;
44         length ++;
45     }
46     fast = head;
47     k = k % length;
48 //  printf("%d %d\n",k,length);
49
50     while( k > 0 )
51     {
52         fast = fast->next;
53         if( fast == NULL )
54         {
55             return head;
56         }
57         k --;
58     }
59
60     while( fast->next != NULL )
61     {
62         slow = slow->next;
63         fast = fast->next;
64     }
65     fast->next = head;
66     newhead = slow->next;
67     slow->next = NULL;
68
69     return newhead;
70 }
时间: 2024-11-02 18:41:33

LeetCode 61的相关文章

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. 这道题的要求是向右旋转链表k步. 其实就是把链表后面l-k个节点放到前面,可以采用快慢指针处理.不

leetcode 61 Rotate List ----- 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,然后从右向左数第k个节点,然后以这个节点为开头,重新组成一个链表. 需要注意的就是如果k大于链表长度len

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

[LeetCode] 61. 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. 问题:给定列表 和一个整数 k ,旋转列表最后 k 个元素至列表最前面. 关键是找到最后元素 lastOne 和 旋转后列表新的最后元素

[LeetCode]61. Excel Sheet Column Number Excel列序号

Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Credits:Special thanks to @ts for addi

leetcode || 61、Rotate List

problem: 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. Hide Tags Linked List Two Pointers 题意:将倒数总数K个结点反转到前面,研究了一

[C++]LeetCode: 61 Search a 2D Matrix

题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previou

LeetCode(61)-Valid Palindrome

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider

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. 思路:题目很清晰,思路是先得到链表长度,再从头开始直到特定点,开始变换连接即可. 代码如下: /** * D