예제 #1
0
        public void RemoveLast()
        {
            if (Count != 0)
            {
                if (Count == 1)
                {
                    Head = null;
                    Tail = null;
                }
                else
                {
                    ListNode currrent = Head;
                    while (currrent.next != Tail)
                    {
                        currrent.next = null;
                        Tail          = currrent;
                    }
                }

                Count--;
            }
        }
예제 #2
0
        private ListNode getMiddle(ListNode head)
        {
            // Base case
            if (head == null)
            {
                return(head);
            }
            ListNode fastptr = Head.next;
            ListNode slowptr = head;

            // Move fastptr by two and slow ptr by one
            // Finally slowptr will point to middle node
            while (fastptr != null)
            {
                fastptr = fastptr.next;
                if (fastptr != null)
                {
                    slowptr = slowptr.next;
                    fastptr = fastptr.next;
                }
            }
            return(slowptr);
        }