剑指offer22--链表的复制
2016-06-04 22:38
232 查看
题目:请实现函数ComplexListNode clone(ComplexListNode head),复制一个复杂链表。在复杂链表中,每个结点除了有一个next 域指向下一个结点外,还有一个sibling 指向链表中的任意结点或者null。
package 剑指offer;
/*题目:请实现函数ComplexListNode clone(ComplexListNode head),
* 复制一个复杂链表。在复杂链表中,每个结点除了有一个next 域指向下一个结点外,
* 还有一个sibling 指向链表中的任意结点或者null。*/
public class Test26 {
public static void main(String args[]){
ArrayListComp head = new ArrayListComp();
head.value = 1;
head.next = new ArrayListComp();
head.next.value = 2;
head.next.next = new ArrayListComp();
head.next.next.value = 3;
head.next.next.next = new ArrayListComp();
head.next.next.next.value = 4;
head.next.next.next.next = new ArrayListComp();
head.next.next.next.next.value = 5;
head.sibling = head.next.next;
head.next.sibling = head.next.next.next.next.next;
head.next.next.next.sibling = head.next;
ArrayListComp tmp = head;
printList(head);
ArrayListComp newHead = cloneArray(head);
printList(head);
System.out.println(isSame(head, tmp));
printList(newHead);
System.out.println(isSame(head, newHead));
}
public static ArrayListComp cloneArray(ArrayListComp head){
if(head == null){
return null;
}
//先复制结点
cloneNodes(head);
//链接sibling字段
connectNodes(head);
//将链表拆分,返回复制的链接结点
return reconnectNodes(head);
}
public static boolean isSame(ArrayListComp h1, ArrayListComp h2) {
while (h1 != null && h2 != null) {
if (h1 == h2) {
h1 = h1.next;
h2 = h2.next;
} else {
return false;
}
}
return h1 == null && h2 == null;
}
public static void cloneNodes(ArrayListComp head){
while(head != null){
ArrayListComp temp = new ArrayListComp();
temp.value = head.value;
// 这两句是连接作用,将temp连接到head之后
temp.next = head.next;
head.next = temp;
//head结点前移,变成第三个结点
head = temp.next;
}
}
public static void connectNodes(ArrayListComp head){
while(head != null){
if(head.sibling != null){
head.next.sibling = head.sibling.next;
}
head = head.next.next;
}
}
public static ArrayListComp reconnectNodes(ArrayListComp head){
if(head == null){
return null;
}
ArrayListComp newhead = head.next;
ArrayListComp pointer = newhead;
head.next = newhead.next;
head = head.next;
while (head != null) {
// pointer指向复制结点
pointer.next = head.next;
pointer = pointer.next;
// head的下一个指向复制结点的下一个结点,即原来链表的结点
head.next = pointer.next;
// head指向下一个原来链表上的结点
head = pointer.next;
}
// 返回复制链表的头结点
return newhead;
}
public static void printList(ArrayListComp head) {
while (head != null) {
System.out.print(head.value + "->");
head = head.next;
}
System.out.println("null");
}
}
class ArrayListComp{
int value;
ArrayListComp next;
ArrayListComp sibling;
}
使用先创建后复制的方式
package 剑指offer;
/*题目:请实现函数ComplexListNode clone(ComplexListNode head),
* 复制一个复杂链表。在复杂链表中,每个结点除了有一个next 域指向下一个结点外,
* 还有一个sibling 指向链表中的任意结点或者null。*/
public class Test26 {
public static void main(String args[]){
ArrayListComp head = new ArrayListComp();
head.value = 1;
head.next = new ArrayListComp();
head.next.value = 2;
head.next.next = new ArrayListComp();
head.next.next.value = 3;
head.next.next.next = new ArrayListComp();
head.next.next.next.value = 4;
head.next.next.next.next = new ArrayListComp();
head.next.next.next.next.value = 5;
head.sibling = head.next.next;
head.next.sibling = head.next.next.next.next.next;
head.next.next.next.sibling = head.next;
ArrayListComp tmp = head;
printList(head);
ArrayListComp newHead = cloneArray(head);
printList(head);
System.out.println(isSame(head, tmp));
printList(newHead);
System.out.println(isSame(head, newHead));
}
public static ArrayListComp cloneArray(ArrayListComp head){
if(head == null){
return null;
}
//先复制结点
cloneNodes(head);
//链接sibling字段
connectNodes(head);
//将链表拆分,返回复制的链接结点
return reconnectNodes(head);
}
public static boolean isSame(ArrayListComp h1, ArrayListComp h2) {
while (h1 != null && h2 != null) {
if (h1 == h2) {
h1 = h1.next;
h2 = h2.next;
} else {
return false;
}
}
return h1 == null && h2 == null;
}
public static void cloneNodes(ArrayListComp head){
while(head != null){
ArrayListComp temp = new ArrayListComp();
temp.value = head.value;
// 这两句是连接作用,将temp连接到head之后
temp.next = head.next;
head.next = temp;
//head结点前移,变成第三个结点
head = temp.next;
}
}
public static void connectNodes(ArrayListComp head){
while(head != null){
if(head.sibling != null){
head.next.sibling = head.sibling.next;
}
head = head.next.next;
}
}
public static ArrayListComp reconnectNodes(ArrayListComp head){
if(head == null){
return null;
}
ArrayListComp newhead = head.next;
ArrayListComp pointer = newhead;
head.next = newhead.next;
head = head.next;
while (head != null) {
// pointer指向复制结点
pointer.next = head.next;
pointer = pointer.next;
// head的下一个指向复制结点的下一个结点,即原来链表的结点
head.next = pointer.next;
// head指向下一个原来链表上的结点
head = pointer.next;
}
// 返回复制链表的头结点
return newhead;
}
public static void printList(ArrayListComp head) {
while (head != null) {
System.out.print(head.value + "->");
head = head.next;
}
System.out.println("null");
}
}
class ArrayListComp{
int value;
ArrayListComp next;
ArrayListComp sibling;
}
使用先创建后复制的方式
相关文章推荐
- 前端资源汇总
- html事件处理程序
- Mysql删除或者更新出错 Error Code: 1175. You are using safe update
- HTML学习笔记
- 《CSS权威指南》学习记录——行内元素
- PHP前端和数据库的连接
- js网页如何获取手机屏幕宽度
- HTML meta viewport属性详细说明
- ReactiveCocoa 用 RACSignal 替代 Delegate
- 12个JavaScript技巧
- 图解css position问题
- JavaScript 事件
- [翻译][Nokogiri官方教程] 解析HTML/XML文档 / Parsing an HTML/XML Document
- (8)css表格
- jQuery EasyUI时间戳自定义时间显示格式类型
- html列表相关属性
- css美化number类型输入框美化实现自定义+、-号点击按钮增减效果
- reactjs入门到实战(三)---- 组件详解
- React Native Package Manager包管理器介绍
- 237. Delete Node in a Linked Lis