复制含有随机指针节点的链表
public class CopyLinkedWithRandom {
public static void main(String[] args) {
SpecialNode node1 = new SpecialNode(1);
SpecialNode node2 = new SpecialNode(2);
SpecialNode node3 = new SpecialNode(3);
node1.next = node2;
node1.rand = node3;
node2.next = node3;
node2.rand = node3;
node3.next = null;
node3.rand = null;
SpecialNode copy01 = copyLinkedWithRan01(node1);
while(copy01 != null) {
System.out.println(copy01.value+"next:"+(copy01.next== null?"null":copy01.next.value)+"rand:"+(copy01.rand== null?"null":copy01.rand.value));
copy01 = copy01.next;
}
System.out.println("----------");
SpecialNode copy02 = copyLinkedWithRan02(node1);
while(copy02 != null) {
System.out.println(copy02.value+"next:"+(copy02.next== null?"null":copy02.next.value)+"rand:"+(copy02.rand== null?"null":copy02.rand.value));
copy02 = copy02.next;
}
}
public static SpecialNode copyLinkedWithRan01(SpecialNode head) {
if(head == null) {
return null;
}
HashMap<SpecialNode,SpecialNode> map = new HashMap<SpecialNode,SpecialNode>();
SpecialNode cur = head;
while(cur != null) {
map.put(cur, new SpecialNode(cur.value));
cur = cur.next;
}
cur = head;
while(cur != null) {
map.get(cur).next = map.get(cur.next);
map.get(cur).rand = map.get(cur.rand);
cur = cur.next;
}
return map.get(head);
}
public static SpecialNode copyLinkedWithRan02(SpecialNode head) {
if(head == null) {
return null;
}
SpecialNode cur = head;
SpecialNode next = null;
while(cur != null) {
next = cur.next;
cur.next = new SpecialNode(cur.value);
cur.next.next = next;
cur = next;
}
cur = head;
while(cur != null) {
cur.next.rand = cur.rand==null?null:cur.rand.next;
cur = cur.next.next;
}
cur = head;
SpecialNode newNode = head.next;
SpecialNode cur2 = newNode;
while(cur != null) {
cur.next = cur.next.next;
cur2.next = cur.next==null?null:cur.next.next;
cur = cur.next;
cur2 = cur2.next;
}
return newNode;
}
}
class SpecialNode{
public SpecialNode next;
public SpecialNode rand;
public int value;
public SpecialNode(int value) {
this.value = value;
}
}
private static Node copy2(Node head) {
Node n1 = head;
Node n2 = null;
while(n1 != null){
n2 = n1.next;
n1.next = new Node(n1.value);
n1.next.next = n2;
n1 = n2;
}
n1 = null;
n2 = head;
Node n3 = null;
Node n = head.next;
int i = 0;
while(n2 != null){
if(n2.next == null){
n2.random = n1.random.next != null? n1.random.next: null;
n2 = n2.next;
continue;
}
if(i % 2 ==0){
n3 = n2.next;
n2.next = n2.next.next;
n1 = n2;
n2 = n3;
}else{
n2.random = n1.random.next != null? n1.random.next: null;
n3 = n2.next;
n2.next = n2.next.next;
n1 = n2;
n2 = n3;
}
i++;
}
return n;
}
两个单链表生成相加链表
public class AddLinked {
public static void main(String[] args) {
Node node1 = new Node(9);
Node node2 = new Node(3);
Node node3 = new Node(7);
node1.next = node2;
node2.next = node3;
node3.next = null;
Node node4 = new Node(6);
Node node5 = new Node(3);
node4.next = node5;
node5.next = null;
Node addLinked = addLinked02(node1,node4);
while(addLinked != null) {
System.out.println(addLinked.value);
addLinked = addLinked.next;
}
}
public static Node addLinked01(Node head1, Node head2) {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
while (head1 != null) {
stack1.push(head1.value);
head1 = head1.next;
}
while (head2 != null) {
stack2.push(head2.value);
head2 = head2.next;
}
int stack1Value = 0;
int stack2Value = 0;
int sum = 0;
int full = 0;
Node last = null;
while (!stack1.isEmpty() || !stack2.isEmpty()) {
stack1Value = stack1.isEmpty() ? 0 : stack1.pop();
stack2Value = stack2.isEmpty() ? 0 : stack2.pop();
sum = stack1Value + stack2Value;
Node cur = new Node((sum + full) % 10);
cur.next = last;
last = cur;
full = (sum + full)/10;
}
if(full == 1) {
Node cur = new Node(1);
cur.next = last;
return cur;
}
return last;
}
public static Node addLinked02(Node head1, Node head2) {
head1 = reverseNode(head1);
head2 = reverseNode(head2);
int head1Value = 0;
int head2Value = 0;
int sum = 0;
int full = 0;
Node cur1 = head1;
Node cur2 = head2;
Node last = null;
while(cur1 != null || cur2 != null) {
head1Value = cur1 == null?0:cur1.value;
head2Value = cur2 == null?0:cur2.value;
sum = head1Value + head2Value;
Node n = new Node((sum + full)%10);
n.next = last;
last = n;
full = (sum + full)/10;
cur1 = cur1==null?null:cur1.next;
cur2 = cur2==null?null:cur2.next;
}
if(full == 1) {
Node n = new Node(1);
n.next = last;
last = n;
}
head1 = reverseNode(head1);
head2 = reverseNode(head2);
return last;
}
public static Node reverseNode(Node head) {
Node pre = null;
Node next = null;
while(head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}