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

Commit 779a0f7

Browse files
author
Ram swaroop
committed
move last node to first: done
1 parent 2f71ec1 commit 779a0f7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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: 6/23/15
11+
* @time: 7:52 PM
12+
*/
13+
public class MoveLastNodeToFirst<E extends Comparable<E>> extends SingleLinkedList<E> {
14+
15+
public static <E extends Comparable<E>> void moveLastNodeToFirst(SingleLinkedList<E> list) {
16+
if (list.size <= 1) return;
17+
18+
SingleLinkedNode<E> curr = list.getNode(0), prev = curr;
19+
while (curr.next != null) {
20+
prev = curr;
21+
curr = curr.next;
22+
}
23+
prev.next = null;
24+
curr.next = list.head;
25+
list.head = curr;
26+
}
27+
28+
public static void main(String a[]) {
29+
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
30+
linkedList.add(00);
31+
linkedList.add(11);
32+
linkedList.add(22);
33+
linkedList.add(33);
34+
linkedList.printList();
35+
moveLastNodeToFirst(linkedList);
36+
linkedList.printList();
37+
}
38+
}

0 commit comments

Comments
 (0)