GoConqr has a new look! Let us know what you think.

Flashcards

Rotate List

Suhas S English 1 card

Cards in this deck (1)

  1. 1Given a list, rotate the list to the right by k places, where k is non-negative.public ListNode rotateRight(ListNode head, int k) {
    if(head==null || head.next==null) return head;
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode fast = dummy;
    ListNode slow = dummy;
    int count = 0;
    while(fast.next != null){
    fast = fast.next;
    count++;
    }
    for(int i = count - k % count; i>0; i--){
    slow = slow.next;
    }
    fast.next = dummy.next;
    dummy.next = slow.next;
    slow.next = null;
    return dummy.next;
    }

Information

Description
Flashcards on Rotate List, created by Suhas S on 27/02/2018.
Created by
Suhas S
Views
10
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?