簡體   English   中英

刪除右邊所有具有更大價值的節點(鏈接列表)

[英]remove all the nodes which have greater value on right(Linked list)

給定一個單鏈列表,請刪除右側所有具有較大值的所有節點。 這不是家庭作業,在一次采訪中有人問我。

例如

輸入:

2 ---> 4 ---> 2 ---> 1 ---> 3 ---> 0

那么輸出應該是

4 ---> 3 ---> 0。

輸入:

30 ---> 40 ---> 50 ---> 60

那么輸出應該是

60

我的方法如下:

1. reverse the link list
2. maintain the max value if the current node is less than max than remove else move next
3. reverse again

但是面試官要求我對此進行優化。 我認為他期望的與@Trying相同。

時間復雜度O(N)。 如果您有任何疑問,請告訴我。 謝謝。

Node delete(Node n){
        if(n==null){
            return null;
        }
        Node t = delete(n.next);
        if(t==null){
            return n; // i.e. right most node so just return this
        }
        else{
            Comparable c = (Comparable)n.k;
            if(c.compareTo((Comparable)t.k)<0){ //no need of this node so return t.
                return t;
            }
            else{
                n.next=t; //valid node so change the next pointer of current node
                return n;
            }
        }
    }

拆分問題,然后從解決方案中借用。

decreasingList(List list) {
    if list empty then return empty

    List restSolution = decreasingList(list.next)
    ... list.first ... // What now

問問自己,擁有restSolutionlist.first應該返回什么。

這使得計算機科學比數學有趣得多:懶惰,只做一點工作和委派工作。


對不起以為這是功課

static class List {
    int value;
    List next;

    List(int value, List next) {
        this.value = value;
        this.next = next;
    }

    static List makeList(int... values) {
        List list = null;
        List tail = null;
        for (int value: values) {
            List node = new List(value, null);
            if (tail == null) {
                list = node;
            } else {
                tail.next = node;
            }
            tail = node;
        }
        return list;
    }

    @Override
    public String toString() {
        if (next == null) {
            return String.valueOf(value);
        }
        return String.valueOf(value) + "-->" + next.toString();
    }
}

static List decreasingList(List list) {
    if (list == null) {
        return null;
    }
    List rest = decreasingList(list.next);
    if (rest != null && list.value < rest.value) {
        return rest;
    }
    list.next = rest;
    return list;
}

public static void main(String[] args) {
    List list = List.makeList(2, 4, 2, 1, 3, 0);
    System.out.println("List: " + list);
    list = decreasingList(list);
    System.out.println("Decreasing: " + list);
}

遞歸可以像您一樣解決:通過遍歷下一個並將下一個更改為上一個節點來進行反向。 然后在尾巴上往回走。

static List decreasingList(List list) {
    List prior = null;
    while (list != null) {
        List next = list.next;
        list.next = prior;
        prior = list;
        list = next;
    }
    list = prior; // The reversed list.

    prior = null;
    while (list != null) {
        List next = list.next;
        list.next = prior;
        if (prior == null || list.value > prior.value) {
            prior = list;
        }
        list = next;
    }
    list = prior;
    return list;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM