http://n00tc0d3r.blogspot.com/2013/05/rotate-list.html
Rotate a Linked List Given a singly linked list, rotate the linked list counter-clockwise by k nodes.
For example:
Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.
Given 1->2->3->4->5->NULL and k = 6, return 5->1->2->3->4->NULL.
aaa
https://leetcode.com/problems/rotate-list/discuss/22715/Share-my-java-solution-with-explanation
Read full article from Rotate a Linked List | GeeksforGeeks
Rotate a Linked List Given a singly linked list, rotate the linked list counter-clockwise by k nodes.
For example:
Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.
Given 1->2->3->4->5->NULL and k = 6, return 5->1->2->3->4->NULL.
aaa
There are two cases: k < length-of-list and k >= length-of-list.
In the case of k < length-of-list,
Note that now the tail node is the one whose next is head (not null any more).
In the case of k < length-of-list,
- use two pointers to find the k-to-the-end node,
- link the original tail to the original head,
- then cut the list before k-to-the-end node and make it the new head.
Note that now the tail node is the one whose next is head (not null any more).
public ListNode rotateRight(ListNode head, int k) {
if (head == null || k <= 0 || head.next == null) return head;
ListNode pre = head, end = head;
// find the k-to-the-end
while (end.next != head || k > 0) {
if (k > 0) { // only forward end pointer
--k;
} else { // forward both pointers
pre = pre.next;
}
end = end.next;
// make it a circular linked list
if (end.next == null) end.next = head;
}
ListNode newHead = pre.next;
pre.next = null;
return newHead;
}
In this algorithm, the pilot pointer has been forwarded at least k times and the two pointers were moved together to get to the k-to-the-end node. The worst case running time is O(k+n) where n is the length of the list.
Ask the interviewer, or consider different possible input cases: k >> n.
If k >> n, we can improve the algorithm by first compute the length of the list. By doing this, the running time becomes O(2n) = O(n) since we iterate through the list twice: one for the length and the other for the k-to-the-end node.
Ask the interviewer, or consider different possible input cases: k >> n.
If k >> n, we can improve the algorithm by first compute the length of the list. By doing this, the running time becomes O(2n) = O(n) since we iterate through the list twice: one for the length and the other for the k-to-the-end node.
https://leetcode.com/problems/rotate-list/discuss/22715/Share-my-java-solution-with-explanation
public ListNode rotateRight(ListNode head, int n) {
if (head==null||head.next==null) return head;
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode fast=dummy,slow=dummy;
int i;
for (i=0;fast.next!=null;i++)//Get the total length
fast=fast.next;
for (int j=i-n%i;j>0;j--) //Get the i-n%i th node
slow=slow.next;
fast.next=dummy.next; //Do the rotation
dummy.next=slow.next;
slow.next=null;
return dummy.next;
}
http://rleetcode.blogspot.com/2014/01/rotate-list-java.htmlpublic ListNode rotateRight(ListNode head, int k) {
// find the length of the list
int len = 0; ListNode cur = head;
while (cur != null) {
++len;
cur = cur.next;
}
if (len == 0 || k % len == 0) return head;
k = k % len;
ListNode pre = head; cur = head;
// find the n-to-the-end
while (cur.next != head) {
if (k > 0) { // only forward end pointer
--k;
} else { // forward both pointers
pre = pre.next;
}
cur = cur.next;
// make it a circular linked list
if (cur.next == null) cur.next = head;
}
ListNode newHead = pre.next;
pre.next = null;
return newHead;
}
void
rotate (
struct
node **head_ref,
int
k)
{
if
(k == 0)
return
;
// Let us understand the below code for example k = 4 and
// list = 10->20->30->40->50->60.
struct
node* current = *head_ref;
// current will either point to kth or NULL after this loop.
// current will point to node 40 in the above example
int
count = 1;
while
(count < k && current != NULL)
{
current = current->next;
count++;
}
// If current is NULL, k is greater than or equal to count
// of nodes in linked list. Don't change the list in this case
if
(current == NULL)
return
;
// current points to kth node. Store it in a variable.
// kthNode points to node 40 in the above example
struct
node *kthNode = current;
// current will point to last node after this loop
// current will point to node 60 in the above example
while
(current->next != NULL)
current = current->next;
// Change next of last node to previous head
// Next of 60 is now changed to node 10
current->next = *head_ref;
// Change head to (k+1)th node
// head is now changed to node 50
*head_ref = kthNode->next;
// change next of kth node to NULL
// next of 40 is now NULL
kthNode->next = NULL;
}