package arithmetic;
public class ReverseList {
public static class Node {
public int value;
public Node next;
public Node(int data) {
value = data;
}
}
public static class DoubleNode {
public int value;
public DoubleNode last;
public DoubleNode next;
public DoubleNode(int data) {
value = data;
}
}
public static Node reverseLinkedList(Node head) {
Node pre = null;
Node next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
public static DoubleNode reverseDoubleList(DoubleNode head) {
DoubleNode pre=null;
DoubleNode next=null;
while (head!=null){
next= head.next;
head.next=pre;
head.last=next;
pre=head;
head=next;
}
return pre;
}
public static Node removeValue(Node head, int num) {
while (head!=null){
if (head.value!=num){
break;
}
head= head.next;
}
//pre为上一个不为num的节点
Node pre=head;
//head不动了,最后返回用head,操作当前的cur
//找到第一个不需要删的位置
Node cur=head;
while (cur!=null){
if (cur.value==num){
//移除cur元素,将上一节点pre的next指针指向cur的next指针
pre.next=cur.next;
}else{
pre=cur;
}
//移动一位
cur=cur.next;
}
return head;
}
}