LinkedList——No.203 Remove Linked List Elements

Problem:

Remove all elements from a linked list of integers that have value val.

Explanation:

删除链表中所有指定值的结点。

My Thinking:

遍历结点并判断即可。

My Solution:

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode fakehead=new ListNode(-1);
        fakehead.next=head;
        ListNode returnhead=fakehead;
        while(fakehead!=null && fakehead.next!=null){
            if(fakehead.next.val==val){
                fakehead.next=fakehead.next.next;//这里不需要向后移,因为删除结点相当于向后移了
            }else{
                fakehead=fakehead.next;
            }  
        }
        return returnhead.next;
    }
}

Optimum Thinking:

使用递归

Optimum Solution:

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) return null;
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next : head;
    }
}

 

当然可以,以下是使用C++实现单向链表的基本结构,包括头文件`list.h`、`linkedlist.h`,以及实现文件`linkedlist.cpp`和`main.cpp`的简单示例: **list.h**(声明通用的数据元素类型和链表节点类型) ```cpp #ifndef LIST_H_ #define LIST_H_ #include <iostream> // 定义数据元素类型 template <typename T> class DataElement { public: T value; DataElement(T val = T()) : value(val) {} }; // 链表节点类型 template <typename T> class ListNode { public: T data; ListNode* next; ListNode(T val = T()) : data(val), next(nullptr) {} }; #endif // LIST_H_ ``` **linkedlist.h**(链表类和操作声明) ```cpp #ifndef LINKEDLIST_H_ #define LINKEDLIST_H_ #include "list.h" template <typename T> class LinkedList { private: ListNode<T>* head; public: LinkedList() : head(nullptr) {} // 插入元素到列表头部 void insertFront(T value); // 删除指定值的第一个元素 void remove(const T& value); // 打印链表 void printList(); }; template <typename T> void LinkedList<T>::insertFront(T value) { ListNode<T> newNode(value); newNode.next = head; head = &newNode; } template <typename T> void LinkedList<T>::remove(const T& value) { if (head && head->data == value) { ListNode<T>* temp = head; head = head->next; delete temp; } else { ListNode<T>* current = head; while (current->next && current->next->data != value) current = current->next; if (current->next) delete current->next; } } template <typename T> void LinkedList<T>::printList() { ListNode<T>* temp = head; while (temp) { std::cout << temp->data << " "; temp = temp->next; } std::cout << "\n"; } #endif // LINKEDLIST_H_ ``` **linkedlist.cpp**(实现函数) ```cpp #include "linkedlist.h" template <typename T> void LinkedList<T>::insertFront(T value) { // 实现已定义在linkedlist.h中的insertFront函数 } template <typename T> void LinkedList<T>::remove(const T& value) { // 实现已定义在linkedlist.h中的remove函数 } template <typename T> void LinkedList<T>::printList() { // 实现已定义在linkedlist.h中的printList函数 } ``` **main.cpp**(主程序,测试链表) ```cpp #include "list.h" #include "linkedlist.h" int main() { LinkedList<int> list; list.insertFront(5); list.insertFront(3); list.insertFront(1); list.printList(); // 输出: 1 3 5 list.remove(3); list.printList(); // 输出: 1 5 return 0; } ``` 在这个例子中,`main.cpp`创建了一个整数链表,插入一些元素,然后打印链表内容并删除特定元素。注意,`list.h`和`linkedlist.h`是模板类,这意味着它们可以用于处理任何类型的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值