首页 > 试题广场 >

已经提供下面数据结构,表示单个链表节点 ...

[问答题]
已经提供下面数据结构,表示单个链表节点
    class Node {
        public int value;
        public Node next;
 
        public Node(int value) {
            this.value = value;
            this.next = null;
        }
    }
  
    // 实现下面方法,对单链表,进行升序排列
    Node sortList(Node head);
  def sortList(self, head):
        if head is None or head.next is None:
            return head
        mid = (head.val + head.next.val) / 2
        if head.val > head.next.val:
            lhead, rhead = head.next, head
        else:
            lhead, rhead = head, head.next
        lit, rit = lhead, rhead
        it = head.next.next      
        while it is not None:
            if it.val > mid:
                rit.next = it
                rit = it               
            else:
                lit.next = it
                lit = it
            it = it.next
        lit.next, rit.next = None, None
        lhead = self.sortList(lhead)
        rhead = self.sortList(rhead)
        it = lhead
        while it.next is not None:
            it = it.next
        it.next = rhead
        return lhead


发表于 2018-08-02 11:05:22 回复(0)
111
发表于 2019-09-09 02:10:02 回复(0)
public Node sortList(Node head) {
    int temp ;
    Node curNode  = head;
    while(curNode != null){
        Node nextNode = curNode.next;
        while(nextNode != null){
            if (nextNode.value < curNext.value){
                temp = nextNode.value;
                nextNode.value = curNode.value;
                curNode.value = temp; 
            }
            nextNode = nextNode.next;
        }
        curNode = curNext.next;
    }
    return head;
}
发表于 2018-07-20 10:07:01 回复(0)