示例#1
0
        /* L: L0→L1→…→Ln-1→Ln to  L0→Ln→L1→Ln-1→L2→Ln-2→..*/
        /// <summary>
        /// 1. empty
        /// 2.only one element
        /// 3.
        /// </summary>
        /// <param name="node"></param>
        public linknode reorderlist(linknode head)
        {
            if (head == null || head.next == null)
            {
                return(head);
            }
            linknode start = null, res = null;

            start = head;
            res   = start;


            while (start != null)
            {
                linknode tail = start, prevtail = null;
                while (tail.next != null)
                {
                    prevtail = tail;
                    tail     = tail.next;
                }
                if (start == tail || start.next == tail)
                {
                    break;
                }
                linknode tx = start.next;
                start.next    = tail;
                tail.next     = tx;
                prevtail.next = null;
                start         = tx;
            }
            return(res);
        }
示例#2
0
        public void insert(int x)
        {
            linknode temp = new linknode(x);

            if (head == null)
            {
                head = temp;
            }
            else
            {
                tail.next = temp;
            }
            tail = temp;
        }
示例#3
0
        public linknode getmiddle(linknode head)
        {
            if (head == null || head.next == null)
            {
                return(head);
            }
            linknode slow = head, fast = head.next;

            while (fast != null && fast.next != null)
            {
                fast = fast.next.next;
                slow = slow.next;
            }
            return(slow);
        }
示例#4
0
        /*
         * follow same as merge sort.
         * 1. divide the head into tow halves
         */
        public linknode sortlinkedlist(linknode head)
        {
            if (head == null || head.next == null)
            {
                return(head);
            }
            linknode middle     = getmiddle(head);
            linknode nextmiddle = middle.next;

            middle.next = null;
            linknode left  = sortlinkedlist(head);
            linknode right = sortlinkedlist(nextmiddle);
            linknode res   = mergesortlinkedlist(left, right);

            return(res);
        }
示例#5
0
        public linknode mergesortlinkedlist(linknode leftstart, linknode righstart)
        {
            linknode res = null;

            if (leftstart == null)
            {
                return(righstart);
            }
            if (righstart == null)
            {
                return(leftstart);
            }
            if (leftstart.item <= righstart.item)
            {
                res      = leftstart;
                res.next = mergesortlinkedlist(leftstart.next, righstart);
            }
            else
            {
                res            = righstart;
                righstart.next = mergesortlinkedlist(leftstart, righstart.next);
            }
            return(res);
        }
示例#6
0
 public linknode(int x)
 {
     item = x;
     next = null;
 }