public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
**
public boolean addOne(ListNode head, int val) {
ListNode newNode = new ListNode(val);
if (head == null) {
head = newNode;
return true;
}
ListNode tmp = head;
while (tmp.next != null) {
tmp= tmp.next;
}
tmp.next = newNode ;
return true;
}
**
public void insertListNode(ListNode head, ListNode node, int pos){
if(head == null){
head = new ListNode();
}
ListNode tmp = head;
int count = 0;
while(tmp.next != null){
if(pos == count){
node.next = tmp.next;
tmp.next = node;
return;
}
count++;
}
tmp.next = node;
}
**
public boolean deleteOne(ListNode head, int val){
if(head == null){
return false;
}
if(head.val == val){
head = head.next;
return true;
}
ListNode tmp = head;
while (tmp.next != null){
if(tmp.next.val == val){
tmp.next = tmp.next.next;
return true;
}
tmp = tmp.next;
}
return false;
}
**
public int getLength(ListNode head){
int len = 0;
ListNode tmp = head;
while(tmp != null){
len++;
tmp = tmp.next;
}
return len;
}
**
public int findNode(ListNode head, int val){
ListNode tmp = head;
int index = 0;
while(tmp.next != null){
if(tmp.val != val){
index++;
tmp = tmp.next;
}
return index;
}
return -1;
}
**
public ListNode getNode(ListNode head, int index){
if(head == null){
return null;
}
ListNode tmp = head;
for (int i=0; i<index; i++) {
if (tmp.next == null) {
return null;
}
tmp = tmp.next;
}
return tmp;
}
**
public ListNode reverse(ListNode head){
ListNode tmp = head;
ListNode result = null;
if(head == null){
return null;
}
while(tmp != null) {
ListNode ln = tmp.next;
tmp.next = result;
result = tmp;
tmp= ln;
}
return result;
}