Пример #1
0
        public NodeLL Reorder(NodeLL start)
        {
            NodeLL         current = start;
            NodeLL         dummy   = new NodeLL(0);
            Stack <NodeLL> s       = new Stack <NodeLL>();
            int            length  = GetLength(start);
            int            mid     = length / 2;

            for (int i = 0; i < mid; i++)
            {
                start = start.next;
            }
            for (int i = mid; i < length; i++)
            {
                s.Push(start);
                start = start.next;
            }
            while (current != null)
            {
                dummy.next = current;
                NodeLL stack = s.Pop();
                current.next = stack;
                current      = current.next;
            }
            return(current);
        }
Пример #2
0
        public int GetLength(NodeLL start)
        {
            int count = 0;

            while (start != null)
            {
                count++;
                start = start.next;
            }
            return(count);
        }
Пример #3
0
        public void RemoveDuplicates(NodeLL start)
        {
            HashSet <int> map  = new HashSet <int>();
            NodeLL        prev = null;

            while (start != null)
            {
                if (map.Contains(start.data))
                {
                    prev.next = start.next;

                    start = start.next;
                }
                else
                {
                    map.Add(start.data);
                    prev  = start;
                    start = start.next;
                }
            }
        }