题目:
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->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL
Example 2:
Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
解法1:
快慢指针法
首先k的长度可以大于链表的长度,所以首先要遍历一遍列表获取链表的长度n,然后k对n取余
然后进行快慢指针法,快慢指针指的是一遍确定倒数第k个节点
如k=2,即确定4的位置
首先快慢指针都处在head的位置,快指针先走k步,慢指针不走
然后快慢指针一起走,等快指针走到最后一个节点时,慢指针即到了目标节点的前一个结点,如在本题中就是3
这里有个特殊情况:如果倒数第k个节点就是head节点,则慢指针怎么也走不到目标节点的前一个节点
两种处理方式:
- 在头节点前再加一个节点dummy
- 对这种情况进行特判:
if(fast == nullptr) return head;
c++:
/**
* 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 == nullptr || k == 0) return head;
ListNode* cur = head;
int n = 0;
while(cur){
n++;
cur = cur->next;
}
k %= n;
ListNode* fast = head;
ListNode* slow = head;
while(k--){
fast = fast->next;
}
if(fast == nullptr) return head;
while(fast->next){
fast = fast->next;
slow = slow->next;
}
fast->next = head;
fast = slow->next;
slow->next = nullptr;
return fast;
}
};
java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || k == 0) return head;
ListNode cur = head;
int n = 0;
while(cur != null){
n++;
cur = cur.next;
}
k %= n;
ListNode fast = head;
ListNode slow = head;
while(k-- != 0){
fast = fast.next;
}
if(fast == null) return head;
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
fast.next = head;
fast = slow.next;
slow.next = null;
return fast;
}
}
python:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if head is None or k == 0:
return head
cur = head
n = 0
while cur != None:
n += 1
cur = cur.next
k %= n
fast = slow = head
for i in range(k):
fast = fast.next
if fast is None: return head
while fast.next != None:
fast = fast.next
slow = slow.next
fast.next = head
fast = slow.next
slow.next = None
return fast
解法2:
由于之前已经遍历过链表过一遍,所以用快慢指针就没有意义,我们可以用一个指针就能确定倒数第k个节点的位置
走的步数为
int m = n - k%n;
c++:
/**
* 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 == nullptr || k == 0) return head;
ListNode* cur = head;
int n = 1;
while(cur->next){
n++;
cur = cur->next;
}
int m = n - k%n;
cur->next = head;
while(m--){
cur = cur->next;
}
ListNode* newhead = cur->next;
cur->next = nullptr;
return newhead;
}
};
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || k == 0) return head;
ListNode cur = head;
int n = 1;
while(cur.next != null){
n++;
cur = cur.next;
}
int m = n - k%n;
cur.next = head;
while(m-- != 0){
cur = cur.next;
}
ListNode newhead = cur.next;
cur.next = null;
return newhead;
}
}
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if head is None or k == 0:
return head
cur = head
n = 1
while cur.next != None:
n += 1
cur = cur.next
m = n - k % n
cur.next = head
for i in range(m):
cur = cur.next
newhead = cur.next
cur.next = None
return newhead