leetcode #61 in cpp

原创 2016年05月30日 11:26:47

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.


Solution: 

If k < length of the node, then we use two pointers to find the new head in the list as what we do in #19, and then we delete the link between the new head and its parent, and add link from the tail to the original head. 

1->2->3->4, say 3 is the new head


1->2->null 3->4----

|______________|

link from tail to original head


If k >= length of the node. we rotate k%length. It is simply like rotating a circle.

Code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(!head) return head;
        if(!head->next) return head;
        
        int len = 0;
        ListNode *cur = head;
        
        while(cur){//calculate how long the linked list is
            len++;
            cur = cur->next;
        }
        
        k %=len;
        if(k==0) return head;
        
        ListNode *prev = head;
        cur = head;
        len =0;
        while(len<k && cur->next){//separate cur k distance from prev
            cur = cur->next;
            len++;
        }
        while(cur->next){//move cur to tail. prev->next is the beginning of the section we are to rotate
            cur = cur->next;
            prev = prev->next;
        }
        cur->next = head;//link tail to original head
        ListNode *newHead = prev->next;//get new head
        prev->next = NULL;//put prev as tail
        
        return newHead;
        
    }
};


相关文章推荐

Leetcode 61. Rotate List (Medium) (cpp)

Leetcode 61. Rotate List (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年08月15日 11:25
  • 171

leetcode #36 in cpp.

The question is to determine a Sudoku is valid.  Solution:  if a Sudoku is valid, each row, each co...

*leetcode 138 in cpp

A linked list is given such that each node contains an additional random pointer which could point t...

leetcode #17 in cpp

The question is : Given a digit string, return all possible letter combinations that the number coul...

leetcode $42 in cpp

Code: (This method is not the best one. There are other methods using two pointers) Key: a bar coul...

*leetcode #80 in cpp

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, G...

Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)

Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月27日 02:35
  • 212

Leetcode 25. Reverse Nodes in k-Group (Hard) (cpp)

Leetcode 25. Reverse Nodes in k-Group (Hard) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月10日 04:04
  • 157

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年08月23日 11:17
  • 272

leetcode #60 in cpp

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #61 in cpp
举报原因:
原因补充:

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