复制链表

class LinkedList
    {
        private LinkNode predix = new LinkNode(){next = null};

        public void Insert(int val)
        {
            LinkNode current = predix;
            while (current.next != null)
            {
                current = current.next;
            }
            current.next = new LinkNode(){value = val,next = null};
        }

        public void Show()
        {
            LinkNode current = predix.next;
            while (current != null)
            {
                Console.WriteLine(current.value);
                current = current.next;
            }
        }

        public LinkedList Copy()
        {
            LinkedList newList = new LinkedList();
            newList.predix = CopyNode(predix);
            return newList;
        }

        public LinkNode CopyNode(LinkNode node)
        {
            if (node == null)
            {
                return null;
            }
            else
            {
                LinkNode newNode = new LinkNode();
                newNode.value = node.value;
                newNode.next = CopyNode(node.next);
                return newNode;
            }
        }
    }

你可能感兴趣的:(复制链表)