英文:
how to solve this problem by using LinkedList in java?
问题
public class LinkedList
{
    Node head;
    public void insert(int data)
    {
        Node node = new Node(data);
        node.data = data;
        // 省略插入逻辑部分
    }
    public void funA(Node head)
    {
        Node current = head;
        int x = 0;
        while (current != null)
        {
            int data = current.data;
            if (data > 3)
            {
                System.out.println(data);
            }
            current = current.next; // 修正为 current.next
        }
    }
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();
        list.insert(5);
        list.insert(2);
        list.insert(10);
        list.insert(3);
    }
}
英文:
public class LinkedList
{
	Node head;
	public void insert(int data)
	{
		Node node=new Node(data);
		node.data=data;
		head
	}
	public void funA(Node head)
	{
		Node current=head;
		int x=0;
		while(current!=null)
		{
			int data=current.data;
			if(data>3)
			{
				System.out.println(data);
			}
			current=current.nextLine;
		}	
	}
		public static void main(String[] args)
		{
			LinkedList list=new LinkedList();
			list.insert(5);
			list.insert(2);
			list.insert(10);
			list.insert(3);
		}
}
Output when :
funA(5-> 2 -> 10 ->3)
答案1
得分: 1
public class LinkedList {
    Node head;
    public void insert(int data) {
        if (head == null) {
            head = new Node(data);
        } else {
            Node node = new Node(data);
            Node temp = head;
            while (temp.nextLine != null) {
                temp = temp.nextLine;
            }
            temp.nextLine = node;
        }
    }
    public void funA(Node head) {
        Node current = head;
        int x = 0;
        while (current != null) {
            int data = current.data;
            if (data > 3) {
                System.out.println(data);
            }
            current = current.nextLine;
        }
    }
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insert(5);
        list.insert(2);
        list.insert(10);
        list.insert(3);
        list.funA(list.head);
    }
    class Node {
        int data;
        Node nextLine;
        public Node(int data) {
            this.data = data;
        }
    }
}
英文:
Can you see if this works --
public class LinkedList {
    Node head;
    public void insert(int data) {
        if (head == null) {
            head = new Node(data);
        } else {
            Node node = new Node(data);
            Node temp = head;
            while (temp.nextLine != null) {
                temp = temp.nextLine;
            }
            temp.nextLine = node;
        }
    }
    public void funA(Node head) {
        Node current = head;
        int x = 0;
        while (current != null) {
            int data = current.data;
            if (data > 3) {
                System.out.println(data);
            }
            current = current.nextLine;
        }
    }
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insert(5);
        list.insert(2);
        list.insert(10);
        list.insert(3);
        list.funA(list.head);
    }
    class Node {
        int data;
        Node nextLine;
        public Node(int data) {
            this.data = data;
        }
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论