Java 删除偶数索引';链表中的值

Java 删除偶数索引';链表中的值,java,linked-list,nodes,Java,Linked List,Nodes,我正在编写一个方法RemoveEvents,它从列表中删除偶数索引中的值,并返回一个新列表,其中包含按原始顺序排列的值。交易是我正在实现我自己的链表,我不能使用java.util中的LinkedList 例如,如果变量list1存储以下值: **list1: [8, 13, 17, 4, 9, 12, 98, 41, 7, 23, 0, 92]** And the following call is made: **LinkedIntList list2 = list1.removeEvens

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

例如,如果变量
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
    ...
}
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;
}
ListNode类中的字段:

public int data;          // data stored in this node
public ListNode next;     // link to next node in the list
我的代码(已更新)

public class LinkedIntList {
    private ListNode front;   // null for an empty 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;
}
我的输出

更新的输出:

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


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

一种方法是返回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;
}
public List removevens(){
List 2dOutput=newarraylist();
LinkedIn列表奇数=新LinkedIn列表();
LinkedIntList偶数=新的LinkedIntList();
ListNode温度=前;
偶数。前=温度;
如果(front==null){
System.out.println(“输入的列表为空”);
}else if(front.next==null){
odd.front=null;
front=null;
}否则{
ListNode当前=front.next;
奇数前=电流;
while(temp!=null){
current.next=current.next.next;
当前=当前。下一步;
temp.next=temp.next.next;
温度=下一个温度;
}
}
2dOutput.add(偶数);
2dOutput.add(奇数);
返回输出;
}
值得一提的是,我还没有测试过这一点,但我非常确定这将成功地将两者分开

我不确定你最后发布的输出字段是什么,所以不管它是什么,你可能希望它的格式有点不同


祝你好运

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

最初,您有:

1 -> 2 -> 3 -> 4
要拥有两个单独的节点列表,您需要:

  • 维护对节点
    1
    2
  • 更新链接,使
    1->3
    2->4
  • 我假设在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;
    }
    

    这是您的问题的测试代码。 当你打电话给我的时候。列表将只包含偶数,而新列表b将只包含奇数

    公共类链接列表 { 私有节点前端

    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();
    
        }
    
    public LinkedIntList removevens()
    {
    LinkedIn列表b=新的LinkedIn列表();
    ListNode温度=前;
    整数计数=0;
    ListNode evenprov=null;
    ListNode oddLast=null;
    while(temp!=null)
    {
    如果(计数%2==0)
    {
    evenPrev=温度;
    温度=下一个温度;
    }
    其他的
    {
    if(oddLast==null)
    {
    b、 前=温度;
    oddLast=温度;
    }
    其他的
    {
    oddLast.next=温度;
    oddLast=oddLast.next;
    }
    evenprov.next=下一个温度;
    温度=下一个温度;
    oddLast.next=null;
    }
    计数++;
    }
    返回b;
    }
    公共空间显示()
    {
    ListNode温度=前;
    while(temp!=null)
    {
    系统输出打印LN(温度数据);
    温度=下一个温度;
    }
    }
    公共静态void main(字符串[]args)
    {      
    LinkedIn列表a=新的LinkedIn列表();
    ListNode aLast=null;
    对于(int i=0;i请尝试以下方法:

    public LinkedIntList removeEvens () {
        LinkedIntList list2 = new LinkedIntList();
    
        for (int i = 0; i < size(); i++) {
            list2.add(get(i));
            remove(i);
        }    
        return list2;
    }
    
    public LinkedIntList removevens(){
    LinkedIntList list2=新的LinkedIntList();
    对于(int i=0;i
    Hi,谢谢你的提示。我理解逻辑,但很难一直跟踪所有这些节点。作为初学者,你是否发现你将even.temp设置为与
    front
    相同的元素?也许应该是
    front。下一步
    。Hi,谢谢你的努力。但是在这种情况下,我只能返回包含值的新列表偶数索引的es。我正在查看您的代码,看看是否可以修复任何问题。再次感谢。您好,谢谢。有一个add方法,我使用您的代码更新了我的代码,现在似乎将奇数和偶数索引分开,但它们仍然在同一个列表中。我还更新了输出。我忘记了更新原始列表的大小。我编辑了answer。您的代码不需要b.front=front,因为这是addTail()应该做的。谢谢。这帮助我解决了我的问题。稍微修改了代码。欢迎使用。需要考虑的一点是,如何使用通用代码删除备用节点,以便RemoveEvents()和RemoveOffics()可以调用该方法来完成所有操作。祝您好运!