Friday, 28 December 2012
Posted by Animesh Shaw

Videos on Digital Electronics - 2 Flip-Flops

Thursday, 20 December 2012
Posted by Animesh Shaw

Reverse linked list



This is one of the questions asked in Microsoft Interview.
Given a linked list, we need to write a function that reverses the nodes of a linked list ‘k’ at a time and returns modified linked list.
The following are the constraints given:
  • If the no. of nodes in a link list is not a multiple of k then left-out nodes in the end should remain as it is
  • You have to retain the memory address of the nodes without modifying it i.e. you can’t just interchange the values in the nodes
  • Only constant memory is allowed
For example the linked list given is as follows:
Linked List : 1->2->3->4->5->6->7->8->9->10->11 -> null
For k = 2
Return Value: 2->1->4->3->6->5->8->7->10->9->11 ->null
For k = 3
Return value: 3->2->1->6->5->4->9->8->7->10->11 -> null
The following are the sequence of steps to be followed. In this solution, we will be using the reverse of a linked list solution described in previous post.
  • HeaderNode is the head of the linked list
  • Take three pointers StartNode, EndNode and NextNode
  • Let the NextNode pointer points to HeaderNode and unlink HeaderNode
  • Repeat the following steps until NextNode is null
    • Point StartNode and EndNode to NextNode
    • Move EndNode K nodes away from StartNode
    • Point NextNode to the node next to EndNode
    • Unlink EndNode from the linked list
    • Now reverse the list pointed by StartNode which gives reverse of K nodes
    • If HeaderNode is null point the HeaderNode to reversed list else point the reversed list to the end of the HeaderNode list
  • Hence the list pointed by HeaderNode contains the K- Reverse of a linked list
Consider the following linked list where headerNode is pointing to head of the linked list:

Dry run for the above example:
For k=3:
Step 1:
Initially headerNode, startNode and endNode point to null and nextNode points to head of the list.

Step 2:
In the loop beginning, startNode and endNode points to nextNode.

Step 3:
Now endNode  points to the node which is K nodes away from startNode. As K=3, startNode points to 1 and endNode points to 3.

Step 4:
Save nextNode point to the next node of the endNode so that we can begin the next iteration from nextNode

Step 5:
Now unlink the endNode from the list. And pass the list pointed by startNode to the reverse function

Step 6:
The reversal of the list pointed by startNode is returned as 3 -> 2 -> 1

Step 7:
As the headerNode is null, point startNode to headerNode and repeat the loop. Now the loop is repeated for the rest of the linked list. And the list pointed by startNode gets reversed.

Step 8:
As the headerNode is not null, the last element of the current headerNode list gets linked to the list pointed by startNode

As the nextNode is pointing to null, the while loop ends.
Code for the above solution:


01package ds;
02 
03public class KReverseList
04{
05    public KReverseList()
06    {
07    }
08 
09    public ListNode reverseKListNodes(ListNode headerNode, int k)
10    {
11        // Take 3 pointers startNode, endNode, nextNode pointing to headerNode
12        ListNode nextNode = headerNode;
13        ListNode startNode = null;
14        ListNode endNode = null;
15        headerNode = null;
16 
17        while (nextNode != null)
18        {
19            //  startNode and endNode points to nextNode
20            startNode = nextNode;
21            endNode = nextNode;
22 
23            //  Move endNode pointing towards node after k elements from startNode
24            for (int i = 1; i < k; i++)
25            {
26                endNode = endNode.next;
27 
28                if (endNode == null)
29                {
30                    break;
31                }
32            }
33 
34            // If endNode is not null, then reverse the list starting from startNode to endNode
35            // eles if endNode is null, then there is nothing to reverse
36 
37            if (endNode != null)
38            {
39                // Save the node next to endNode
40                nextNode = endNode.next;
41 
42                //  Unlink the endNode
43                endNode.next = null;
44 
45                // Reverse the list starting from startNode
46                ReverseSingleListIterative reverseListIter = new ReverseSingleListIterative();
47                startNode = reverseListIter.reverseList(startNode);
48            }
49            else
50            {
51                nextNode = null;
52            }
53 
54            //  Point headerNode to the startNode of the first iteration.
55            //  If the headerNode is set, append the list startNode to the headerNode
56            if (headerNode == null)
57            {
58                headerNode = startNode;
59            }
60            else
61            {
62                SingleLinkedList.getLastNode(headerNode).next = startNode;
63            }
64        }
65 
66        return headerNode;
67    }
68 
69    public static void main(String[] args)
70    {
71        /*  Constructing Single Linked List:
72            1 -> 2 ->3 -> 4 ->5 */
73        SingleLinkedList newList = new SingleLinkedList();
74        newList.add(1);
75        newList.add(2);
76        newList.add(3);
77        newList.add(4);
78        newList.add(5);
79 
80        System.out.println("List before reversal");
81        newList.printList();
82 
83        ListNode headerNode = newList.getList();
84 
85        KReverseList kRevList = new KReverseList();
86        headerNode = kRevList.reverseKListNodes(headerNode, 2);
87        newList.setList(headerNode);
88 
89        System.out.println("List after reversal");
90        newList.printList();
91    }
92}
Friday, 14 September 2012
Posted by Animesh Shaw

Popular Post

Total Pageviews

Sign In

Follow by Email

Powered by Blogger.

Like Us on Facebook

Search This Blog

Loading...

Indian Coder

- Copyright © Programming Mania -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -