单链表的头结点插入和删除值为n的节点(Java)

这道题是Shopee社招 O2C岗的面试题,没做出来,当场流下了技术太菜的泪水。

可能当时脑子坏了,一开始就没用头结点,后来想起来要用的时候也懒得用了,看面试官的表情也很无奈,索性跟他说我回去重新想想。

package simple;

public class LinkedList {

    static Node head= new Node(-1);

    public static void main(String[] args) {
        insert(2);
        insert(1);
        insert(2);
        insert(2);
        insert(3);
        insert(2);
        insert(4);

        print();
        delete(2);
        print();
    }

    static class Node {
        int val;
        Node next;

        public Node(int value) {
            val = value;
        }

        @Override
        public String toString() {
            return String.valueOf(val);
        }
    }

    public static void insert(int value) {
        Node node = new Node(value);
        node.next = head.next;
        head.next = node;
    }

    public static void delete(int value) {
        Node lastNode = head;
        Node curNode = head.next;
        if (curNode == null) {
            return;
        }
        while (curNode != null) {
            if (curNode.val == value) {
                lastNode.next = curNode.next;
            } else {
                lastNode = curNode;
            }
            curNode = curNode.next;
        }

    }

    public static void print() {
        Node node = head.next;
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }



}

程序输出:
4 2 3 2 2 1 2 
4 3 1 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
} public interface OnItemLongClickListener { void onItemLongClick(View view, int position); } } ``` item.xml ``` <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns以下是一个Python实现的例子: ```python class Node: def __init__(self, val): self.val =:app="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_width="match val self.next = None def insert_node(L, x): """ 在带头结点的非空单链表_parent" android:layout_height="wrap_content" android:layout_margin="8dp" app:cardCornerRadius="8dpL第一个最大值节点之前插入一个值为x的节点。 在ASCII值最大的字符前插"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="入一个data值为x的节点。 """ max_node = None prev_node = L curr_node = L.next16dp"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android while curr_node: if curr_node.val > x: max_node = curr_node prev_node = curr_node :layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" /> <TextView curr_node = curr_node.next else: prev_node.next = Node(x) prev_node.next.next = curr_node return android:id="@+id/tv_description" android:layout_width="wrap_content" android:layout_height="wrap_content" if not max_node: prev_node.next = Node(x) else: new_node = Node(x) new_node.next android:layout_below="@id/tv_name" android:layout_marginTop="8dp" /> </RelativeLayout> </androidx = max_node prev_node.next = new_node # 示例 L = Node(None) L.next = Node('a') L.next.cardview.widget.CardView> ``` popup_menu.xml ``` <menu xmlns:android="http://schemas.android.com/apk/res/android.next = Node('c') L.next.next.next = Node('d') L.next.next.next.next = Node('e') insert_node(L,"> <item android:id="@+id/action_edit" android:title="编辑" /> <item android:id 'b') curr_node = L.next while curr_node: print(curr_node.val, end=" ") curr_node = curr_node.next ``` 输出: ``` a b c d e ``` 这里实现了一个`Node`类,用于表示="@+id/action_delete" android:title="删除" /> </menu> ``` dialog_edit.xml ``` <LinearLayout xmlns:链表节点。`insert_node`函数接受一个带头结点的非空单链表L和一个值xandroid="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap,它在L第一个最大值节点之前插入一个值为x的节点。如果最大值节点有多_content" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/个,则插入到ASCII值最大的字符前面。在实现,我们使用了三个指针:`prevet_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="名称"_node`指向当前节点的前一个节点,`curr_node`指向当前节点,`max_node`指向第一个最 /> <EditText android:id="@+id/et_description" android:layout_width="match_parent" android:layout大值节点。我们遍历整个链表,找到第一个最大值节点,或者遍历完整个链_height="wrap_content" android:hint="描述" android:layout_marginTop="8dp" /> </LinearLayout> ``` 表都没有找到最大值节点。如果找到了最大值节点,则在它前面插入x;否则MyDatabaseHelper.java ``` public class MyDatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "my_database"; ,在链表末尾插入x。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值