【问题标题】:removing even indexes' values in a linked list删除链表中的偶数索引值
【发布时间】:2014-04-10 03:02:04
【问题描述】:

我正在编写一个方法 removeEvens 从列表中删除偶数索引中的值,返回一个新列表,其中包含这些值的原始顺序。交易是我正在实现我自己的链表,我不能使用来自java.util 的链表。

例如,如果变量list1 存储这些值:

**list1: [8, 13, 17, 4, 9, 12, 98, 41, 7, 23, 0, 92]**
And the following call is made:

**LinkedIntList list2 = list1.removeEvens();** 
After the call, list1 and list2 should store the following values:

**list1: [13, 4, 12, 41, 23, 92]
list2: [8, 17, 9, 98, 7, 0]**

方法在这个链表类中

public class LinkedIntList {
    private ListNode front;   // null for an empty list
    ...
}

ListNode 类的字段:

public int data;          // data stored in this node
public ListNode next;     // link to next node in the list

我的代码(已更新)

public LinkedIntList removeEvens(){
    LinkedIntList b = new LinkedIntList();
    b.front = front;
    if(front == null) {  
        System.out.println("The list inputed it empty");
    }
    else {   
        ListNode even = b.front;
        while(even!=null && even.next!=null ) { 
            int toBeAdded = even.next.data;
            even.next = even.next.next;
            add(toBeAdded);
            even = even.next;
        }
    }
    return b;
}

我的输出

更新的输出:

似乎我已将偶数索引的值存储在新列表 (list2) 中,但我如何将剩余值(奇数索引的值)存储在原始列表 (list1) 中?

【问题讨论】:

    标签: java linked-list nodes


    【解决方案1】:

    我假设您在 LinkedIntList 类中有 addTail() 以在列表末尾添加一个新节点。

    您的代码看起来像这样。没试过,但应该可以。

    public LinkedIntList removeEvens(){
        LinkedIntList b = new LinkedIntList();
    
        if(front == null) {  
            System.out.println("The list inputed it empty");
        }
        else {   
            ListNode current = front.next; // current node always points to an odd node
            b.addTail(front); // add first node (even) to list
            front = current: // front now points to first odd node.
    
            // while there is odd node and the next node (even node) is not null
            while(current!=null && current.next != null) { // need to remove even node
                    ListNode toBeAdded = current.next; // save the node to be removed
                    current.next = current.next.next;
                    b.addTail(toBeAdded);
                    current = current.next;
            }
        }
    
        size -= b.size(); // new list size is old list size minus total removed nodes.
        return b;
    }
    

    【讨论】:

    • 嗨,谢谢。有 add 方法,我用你的代码来更新我的,现在它似乎分开了奇数和偶数索引,但它们仍然在同一个列表中。我还更新了输出。
    • 我忘了更新原始列表的大小。我已经编辑了答案。您的代码不需要 b.front = front 因为 addTail() 应该这样做。
    • 谢谢。这帮助我解决了我的问题。稍微修正了代码。
    • 不客气。要考虑的一件事是如何使用通用代码来删除备用节点,以便 removeEvens() 和 removeOdds() 可以调用该方法来完成所有操作。祝你好运!
    【解决方案2】:

    解决此问题的一种方法是返回 LinkedIntList 类型的列表,然后以二维方式返回两个列表(一个表示奇数,一个表示偶数),如下所示:

    public List<LinkedIntList> removeEvens(){
        List<LinkedIntList> 2dOutput = new ArrayList<>();
        LinkedIntList odd  = new LinkedIntList();
        LinkedIntList even = new LinkedIntList();
        ListNode temp = front;
        even.front=temp;
        if(front == null) {  
            System.out.println("The list inputed is empty");
        }else if(front.next == null){
            odd.front = null;
            front = null;
        }else {
    
            ListNode current =front.next;
            odd.front = current;
            while(temp != null){
                current.next = current.next.next;
                current = current.next;
    
                temp.next = temp.next.next;        
                temp = temp.next;          
            }
        }
        2dOutput.add(even);
        2dOutput.add(odd);
        return 2dOutput;
    }
    

    值得注意的是,我还没有测试过这个,但我很确定这会成功地将两者分开。

    我不确定您最后发布的输出字段是什么,所以无论它是什么,您都可能希望您对其进行一些不同的格式化。

    祝你好运!

    【讨论】:

    • 您好,感谢您的努力。但在这种情况下,我只能返回包含偶数索引值的新列表。我正在查看您的代码,看看我是否可以解决任何问题。再次感谢。
    【解决方案3】:

    您需要更改节点中元素之间的链接,并确保“奇数”列表中的第一个元素与偶数列表中的第一个元素不同。

    最初你有:

    1 -> 2 -> 3 -> 4
    

    要拥有两个单独的节点列表,您需要:

    1. 维护对节点12 的引用
    2. 更新链接,如1-&gt;32-&gt;4

    【讨论】:

    • 您好,感谢您的提示。我理解其中的逻辑,但很难继续跟踪所有这些节点。
    • 作为初学者,您是否发现您将 even.temp 设置为与 front 相同的元素?也许应该是front.next
    【解决方案4】:

    这是您问题的测试代码。 在这里,当您调用 remove 事件时。 a 列表将仅包含偶数,而新列表 b 将仅包含奇数

    公共类LinkedIntList { 私有 ListNode 前端;

    public LinkedIntList removeEvens()
        {
            LinkedIntList b = new LinkedIntList();
            ListNode temp = front;
            int count=0;
    
            ListNode evenPrev=null;
            ListNode oddLast=null;
    
    
            while(temp!=null)
            {
    
            if(count%2==0)
            {
                evenPrev=temp;
                temp=temp.next;
            }
            else
            {
                        if(oddLast==null)
                    {
                    b.front=temp;
                    oddLast=temp;   
                }
                else
                {
                    oddLast.next=temp;
                    oddLast=oddLast.next;
                }
    
                evenPrev.next=temp.next;
                temp=temp.next;
                oddLast.next=null;      
            }
                    count++;
        }
        return b;
    }
    
    public void display()
    {
            ListNode temp=front;
            while(temp!=null)
            {
                System.out.println(temp.data);
                temp=temp.next;
            }
    }
    
    
        public static void main(String []args)
    {      
            LinkedIntList a=new LinkedIntList();
            ListNode aLast=null;
    
            for(int i=0;i<11;i++)
            {
                    ListNode temp=new ListNode();
                    temp.data=i;
                    if(a.front==null)
                    {
                        a.front=temp;
                        aLast=temp;
                    }
                    else
                    {
                        aLast.next=temp;
                        aLast=aLast.next;
                    }
            }
    
            a.display();
    
            LinkedIntList b=a.removeEvens();
    
            a.display();
            b.display();
    
        }
    

    }

    类 ListNode { 公共 int 数据; // 存储在该节点中的数据 下一个公共 ListNode; // 链接到列表中的下一个节点
    }

    【讨论】:

      【解决方案5】:

      试试这个:

      public LinkedIntList removeEvens () {
          LinkedIntList list2 = new LinkedIntList();
      
          for (int i = 0; i < size(); i++) {
              list2.add(get(i));
              remove(i);
          }    
          return list2;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-09
        • 2022-11-14
        • 2010-12-23
        • 1970-01-01
        • 1970-01-01
        • 2022-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多