Sunday, December 14, 2014

Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5


This problem is not a really "hard" one as long as you understand how to reverse lists, which I am always confused about. For that reason, I am posting a reverseList method for my reference.


        
ListNode reverseList(ListNode head)
{
    ListNode newhead = null;
    while (head != null)
    {
 ListNode tmp = head.next;
 head.next = newhead;
 newhead = head;
 head = tmp;
    }
    return new head;

}


1. We get the length of the list.
2. We divide the length by k, and get the number of "reverse" we need. Return boundary conditions.
3. We iterate to reverse the list k times. Remember to track the "tail" and "head" of each group to link them together.


public class ReverseList {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null)
            return null;
        int length = 0;
        ListNode curr = head;
        while (curr != null)
        {
            length++;
            curr = curr.next;
        }
        //in the case where k is greater than the length of the lists, return
        int multi = length / k;
        if (k <= 1 || multi == 0)
            return head;
        ListNode curtail = null;
        ListNode curhead = null;
        ListNode pretail = null;
        curr = head;
        for (int i = 0; i < multi; i++)
        {
            ListNode preNode = null;
            ListNode nextNode = null;
            for (int j = 0; j < k; j++)
            {
                nextNode = curr.next;
                curr.next = preNode;
                preNode = curr;
                if (j == 0) curtail = curr;
                if (j == k - 1) curhead = curr;
                curr = nextNode;
            }
            if (pretail != null)
                pretail.next = curhead;
            else
                head = curhead;
            pretail = curtail;
        }
        //if there are left-out nodes, current tail points to the next node
        // otherwise points to null
        curtail.next = curr;
        return head;
    }
}

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete