forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveLastNodeToFirst.java
38 lines (33 loc) · 987 Bytes
/
MoveLastNodeToFirst.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.rampatra.linkedlists;
import com.rampatra.base.SingleLinkedList;
import com.rampatra.base.SingleLinkedNode;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/23/15
* @time: 7:52 PM
*/
public class MoveLastNodeToFirst {
public static <E extends Comparable<E>> void moveLastNodeToFirst(SingleLinkedList<E> list) {
if (list.size <= 1) return;
SingleLinkedNode<E> curr = list.getNode(0), prev = curr;
while (curr.next != null) {
prev = curr;
curr = curr.next;
}
prev.next = null;
curr.next = list.head;
list.head = curr;
}
public static void main(String[] args) {
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
linkedList.add(00);
linkedList.add(11);
linkedList.add(22);
linkedList.add(33);
linkedList.printList();
moveLastNodeToFirst(linkedList);
linkedList.printList();
}
}