示例#1
0
        public void Print()
        {
            RandomNode p = _head;

            while (p != null)
            {
                Console.WriteLine(p.Key);
                p = p.Next;
            }
        }
示例#2
0
        public RandomNode InsertAtHead(int k)
        {
            RandomNode n = new RandomNode(k);

            if (_head == null)
            {
                _head = n;
            }
            else
            {
                n.Next = _head;
                _head  = n;
            }
            return(_head);
        }
示例#3
0
        public RandomNode CopyRandomList(RandomNode head)
        {
            if (head == null)
            {
                return(head);
            }

            RandomNode p1 = head;

            //Add copy
            while (p1 != null)
            {
                RandomNode n = new RandomNode(p1.Key);
                n.Next  = p1.Next;
                p1.Next = n;
                p1      = n.Next;
            }

            //Fix random nodes
            p1 = head;
            while (p1 != null && p1.Next != null)
            {
                if (p1.Random == null)
                {
                    p1.Next.Random = null;
                }
                else
                {
                    p1.Next.Random = p1.Random.Next;
                }
                p1 = p1.Next.Next;
            }

            //Fix next pointers
            p1 = head;
            RandomNode head2 = p1.Next;

            while (p1 != null && p1.Next != null)
            {
                RandomNode temp = p1.Next;
                p1.Next = temp.Next;
                p1      = temp;
            }
            return(head2);
        }
示例#4
0
        public RandomNode InsertAtEnd(int k)
        {
            RandomNode n = new RandomNode(k);

            if (_head == null)
            {
                _head = n;
            }
            else
            {
                RandomNode p = _head;
                while (p.Next != null)
                {
                    p = p.Next;
                }
                p.Next = n;
            }
            return(_head);
        }
示例#5
0
 public RandomNodeList()
 {
     _head = null;
 }
示例#6
0
 public RandomNode(int k)
 {
     Key    = k;
     Next   = null;
     Random = null;
 }