题目:复制一个复杂链表。在复杂链表中,每个结点除了有一个next指针指向下一个结点外,还有一个sibling指向链表中的任意结点或者null。
解题思路:第一步,根据原始链表的每个结点N创建对应的N’,把N’链接在N的后面;第二步,设置复制出来的结点的sibling;第三步,把奇数位置的结点用next链接起来就是原始链表,把偶数位置的结点用next链接起来就是复制出来的链表。
C#实现:
private static void CloneNodes(LinkListMy pHead) { LinkListMy pNode = pHead; while (pNode != null) { LinkListMy pCloned = new LinkListMy(pNode.Value, pNode.Next); pNode.Next = pCloned; pNode = pCloned.Next; } } private static void ConnectSiblingNodes(LinkListMy pHead) { LinkListMy pNode = pHead; while (pNode != null) { LinkListMy pCloned = pNode.Next; if (pNode.Sibling != null) pCloned.Sibling = pNode.Sibling.Next; pNode = pCloned.Next; } } private static LinkListMy ReconnectNodes(LinkListMy pHead) { LinkListMy pNode = pHead; LinkListMy pClonedHead = null; LinkListMy pClonedNode = null; if (pNode != null) { pClonedHead = pClonedNode = pNode.Next; pNode.Next = pClonedNode.Next; pNode = pNode.Next; } while(pNode != null) { pClonedNode.Next = pNode.Next; pClonedNode = pClonedNode.Next; pNode.Next = pClonedNode.Next; pNode = pNode.Next; } return pClonedHead; } public static LinkListMy Clone(LinkListMy pHead) { CloneNodes(pHead); ConnectSiblingNodes(pHead); return ReconnectNodes(pHead); }
Java实现:
private static void cloneNodes(LinkNode pHead) { LinkNode pNode = pHead; while (pNode != null) { LinkNode pCloned = new LinkNode(pNode.value, pNode.next); pNode.next = pCloned; pNode = pCloned.next; } } private static void connectSiblingNodes(LinkNode pHead) { LinkNode pNode = pHead; while (pNode != null) { LinkNode pCloned = pNode.next; if (pNode.sibling != null) pCloned.sibling = pNode.sibling.next; pNode = pCloned.next; } } private static LinkNode reconnectNodes(LinkNode pHead) { LinkNode pNode = pHead; LinkNode pClonedHead = null; LinkNode pClonedNode = null; if (pNode != null) { pClonedHead = pClonedNode = pNode.next; pNode.next = pClonedNode.next; pNode = pNode.next; } while(pNode != null) { pClonedNode.next = pNode.next; pClonedNode = pClonedNode.next; pNode.next = pClonedNode.next; pNode = pNode.next; } return pClonedHead; } public static LinkNode clone(LinkNode pHead){ cloneNodes(pHead); connectSiblingNodes(pHead); return reconnectNodes(pHead); }
Python实现:
@staticmethod def cloneNodes(pHead): pNode = pHead while pNode != None: pCloned = LinkNode(pNode.m_value, pNode.m_next) pNode.m_next = pCloned pNode = pCloned.m_next @staticmethod def connectSiblingNodes(pHead): pNode = pHead while pNode != None: pCloned = pNode.m_next if pNode.m_sibling != None: pCloned.m_sibling = pNode.m_sibling.m_next pNode = pCloned.m_next @staticmethod def reconnectNodes(pHead): pNode = pHead pClonedHead = None pClonedNode = None if pNode != None: pClonedHead = pClonedNode = pNode.m_next pNode.m_next = pClonedNode.m_next pNode = pNode.m_next while pNode != None: pClonedNode.m_next = pNode.m_next pClonedNode = pClonedNode.m_next pNode.m_next = pClonedNode.m_next pNode = pNode.m_next return pClonedHead @staticmethod def clone(pHead): """ 复杂链表的复制 复制一个复杂链表。在复杂链表中,每个结点除了有一个next指针指向下一个结点外 还有一个sibling指向链表中的任意结点或者null :param pHead: :return: """ LinkNode.cloneNodes(pHead) LinkNode.connectSiblingNodes(pHead) return LinkNode.reconnectNodes(pHead)