[Leetcode] [Java] Rotate List

Source: Internet
Author: User

Title:

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 .

Test Instructions:

Given a list of nodes with a distance of k points from the right boundary, rotate the list and K is non-negative.

Like what:

Given1->2->3->4->5->NULL andk = 2,
return4->5->1->2->3->NULL.

Algorithm Analysis:

* Give a single linked list, and a K value, according to the K value to the right rotation, for example:
* Pay attention to examining, start thinking K point from the left, debug cost so much strength ~ ~

* K is the right-to-left number

* It is also important to note that the value of K may exceed the number of nodes in the list, this time the k is required to take the remainder operation, reposition K in the linked list position. When K takes the remainder 0 o'clock, the total number of nodes is indicated as

* Integer times, this time according to the test instructions linked list does not need to be flipped.
* [1,2,3,4,5] k=1----->[5,1,2,3,4]
* [1,2,3,4,5] k=2----->[4,5,1,2,3]
* [1,2,3,4,5] k=3----->[3,4,5,1,2]
* [1,2,3,4,5] k=4----->[2,3,4,5,1]


Reference http://pisxw.com/algorithm/Rotate-List.html

This solution is still the use of double pointers, the basic idea is to set two pointers, i j The middle difference n , with Walker-runner to the node to be rotated, then the next node is set to the new table header, and the current node is set to the footer. Set the front and back two pointers, and then advance the method called the ruler .

AC Code:
<span style= "FONT-SIZE:12PX;" >/** * Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int x) {* val = x; * Next = Null *} *} */public class Solution {public ListNode rotateright (listnode head, int n) {if (Head==null | | h        Ead.next==null) return head;      ListNode Slow=head;        Define two pointers ListNode Fast=head;         ListNode testhead =head;//Copy linked list, used to count the total number of nodes in the linked list int nodenum=0;    Number of nodes in the linked list int k;         while (testhead!=null) {nodenum++;     Statistics total number of nodes testhead=testhead.next;     } K=n%nodenum;        if (k==0)//If n is exactly the number of nodes, you do not have to flip the list return head;                    for (int num=0;num<k;num++)//k to reposition the node to flip the fast=fast.next after taking the remainder;            while (fast.next!=null) {fast=fast.next;        Slow=slow.next;  } Fast.next=head;        Rotate the Head=slow.next;   Slow.next=null;     return head; }}</span>

Oneself first time of the AC code, really call a disorderly ah, reluctant or record it

<span style= "FONT-SIZE:12PX;"     >public class Solution {public ListNode rotateright (ListNode head, int k) {//1->2->3->4->5->null    4->5->1->2->3->null.    int nodenum=0;    ListNode testhead2;    ListNode testhead3;    ListNode Newlistone;      ListNode Newlisttwo;        ListNode Listtwoend; if (head==null| |    Head.next==null) return head;        ListNode Testhead =head;    while (testhead!=null) {nodenum++;    Testhead=testhead.next;    } K=k%nodenum; if (k>=nodenum| |    k==0) return head;    Testhead2=head;       Testhead3=head;    for (int i=1;i<nodenum-k;i++) Testhead2=testhead2.next;    for (int i=1;i<=nodenum-k;i++) Testhead3=testhead3.next;    Newlisttwo=testhead3;    Testhead2.next=null;      Newlistone=head;    Listtwoend = Newlisttwo;    while (Listtwoend.next!=null) Listtwoend=listtwoend.next;    Listtwoend.next=newlistone;            return newlisttwo; }}</span>



Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] Rotate List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.