41：复制复杂链表

复制复杂链表

public class Offer41 {
public static void main(String[] args) {
ComplexListNode node1 = new ComplexListNode(1);
ComplexListNode node2 = new ComplexListNode(2);
ComplexListNode node3 = new ComplexListNode(3);
ComplexListNode node4 = new ComplexListNode(4);
ComplexListNode node5 = new ComplexListNode(5);
ComplexListNode node6 = new ComplexListNode(6);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = null;
node1.sibling = node4;
node2.sibling = node5;
node3.sibling = node6;
node4.sibling = node1;
node5.sibling = node2;
node6.sibling = node3;
ComplexListNode(node1);
}

public static ComplexListNode ComplexListNode(ComplexListNode node){
if(node==null)return null;
ComplexListNode temp = node;
//形成A-'A'-B-'B'...链表
while (node!=null){
ComplexListNode complexListNode = new ComplexListNode(node.value);
complexListNode.next = node.next;
node.next = complexListNode;
node = complexListNode.next;
}
//给sibling进行赋值
node = temp;
while (node!=null){
if(node.sibling!=null){
node.next.sibling = node.sibling.next;
}
node = node.next.next;
}
//拆分成两条链表
node = temp;
ComplexListNode head = temp;
temp = node.next;

node.next = temp.next;
node = node.next;
while (node!=null){
temp.next = node.next;
temp = temp.next;
node.next = temp.next;
node = node.next;
}

}

}
class  ComplexListNode{
int value;
ComplexListNode next;
ComplexListNode sibling;

public ComplexListNode(int value) {
this.value = value;
}
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

83年老蒜头

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

10-08 508
02-10 1849

11-23 1208
10-03 811
10-25 791
07-26 978
08-10 67
12-09 15
06-25 2545
06-09 4070
12-04 2861
04-02 583
07-02 3943
07-18 150
08-31 590
04-30 235
08-15 51
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客