Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 635eacb

Browse files
author
Ram swaroop
committed
rotate linked list: done
1 parent e3d25e3 commit 635eacb

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

src/me/ramswaroop/linkedlists/AddNumbersInTwoLists.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@ public class AddNumbersInTwoLists {
1414

1515
/**
1616
* Adds two numbers represented by two linked lists {@param list1}
17-
* and {@param list2} and stores them in another list.
17+
* and {@param list2} (where first node is the least significant
18+
* digit) and stores them in another list.
1819
* <p/>
1920
* Example:
2021
* <p/>
2122
* Input:
2223
* First List: 5->6->3 // represents number 365
23-
* Second List: 8->4->2 // represents number 248
24+
* Second List: 8->4->2 // represents number 248
2425
* Output:
2526
* Resultant list: 3->1->6 // represents number 613
2627
* <p/>
2728
* Input:
2829
* First List: 7->5->9->4->6 // represents number 64957
29-
* Second List: 8->4 // represents number 48
30+
* Second List: 8->4 // represents number 48
3031
* Output:
3132
* Resultant list: 5->0->0->5->6 // represents number 65005
3233
*
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package me.ramswaroop.linkedlists;
2+
3+
import me.ramswaroop.common.SingleLinkedList;
4+
import me.ramswaroop.common.SingleLinkedNode;
5+
6+
/**
7+
* Created by IntelliJ IDEA.
8+
*
9+
* @author: ramswaroop
10+
* @date: 7/3/15
11+
* @time: 3:07 PM
12+
*/
13+
public class RotateLinkedList {
14+
15+
/**
16+
* Rotates the {@param list} anti-clockwise by {@param k} nodes.
17+
*
18+
* @param list
19+
* @param k
20+
* @param <E>
21+
*/
22+
public static <E extends Comparable<E>> void rotateCounterClockwise(SingleLinkedList<E> list, int k) {
23+
int clockwiseK = list.size - k;
24+
rotateClockwise(list, clockwiseK);
25+
}
26+
27+
28+
/**
29+
* Rotates the {@param list} clockwise by {@param k} nodes.
30+
*
31+
* Example,
32+
*
33+
* Input: [0,11,22,33,44,55] and k =2
34+
* Output: [22,33,44,55,0,11]
35+
*
36+
* @param list
37+
* @param k
38+
* @param <E>
39+
*/
40+
public static <E extends Comparable<E>> void rotateClockwise(SingleLinkedList<E> list, int k) {
41+
int i = 0;
42+
SingleLinkedNode<E> curr = list.head, end = curr;
43+
44+
// get a pointer to the last node
45+
while (end.next != null) {
46+
end = end.next;
47+
}
48+
49+
// start moving first k nodes from start to end
50+
while (i < k && k < list.size) {
51+
end.next = curr;
52+
end = end.next;
53+
curr = curr.next;
54+
i++;
55+
}
56+
57+
// change head to k+1 node
58+
list.head = curr;
59+
end.next = null;
60+
61+
}
62+
63+
public static void main(String a[]) {
64+
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
65+
linkedList.add(00);
66+
linkedList.add(11);
67+
linkedList.add(22);
68+
linkedList.add(33);
69+
linkedList.add(44);
70+
linkedList.add(55);
71+
linkedList.printList();
72+
rotateClockwise(linkedList, 2);
73+
linkedList.printList();
74+
rotateCounterClockwise(linkedList, 2);
75+
linkedList.printList();
76+
}
77+
}

0 commit comments

Comments
 (0)