# Blog

## ListNode

### reverse ListNode

The ListNode have 5 nodes with the order

``````1, 2, 3, 4, 5
``````

now need reverse the order to

``````5, 4, 3, 2, 1
``````

ListNode

``````public class ListNode {
Integer value;
ListNode next;
}
``````

``````public ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode node = null;
while(current != null){
ListNode next = current.next;
if(next == null){
node = current;
}
current.next = prev;
prev = current;
current = next;
}
return node;
}
``````

### reverse sub ListNode

Assuming the ListNode have 5 nodes with the order

``````1, 2, 3, 4, 5
``````

now need reverse sub n nodes, eg, if n is 2, the reverse result like

``````2, 1, 4, 3, 5
``````

if the n is 3, the reverse result like

``````3, 2, 1, 4, 5
``````

ListNode

``````public class ListNode {
Integer value;
ListNode next;
}
``````

``````
``````

### The last n nodes

The ListNode have 5 nodes with the order

``````1, 2, 3, 4, 5
``````

now need find the the last n nodes, eg, n = 3, the last 3 should be

``````3, 4, 5
``````

ListNode

``````public class ListNode {
Integer value;
ListNode next;
}
``````

``````public ListNode partition(ListNode head, int n){
return null;
}
ListNode second = null;
for(int i = 0;i < n-1;i++){
if(first.next != null){
first = first.next;
}else{
return null;
}
}