17.在单链表中删除指定值的节点

【题目】

给定一个链表的头节点head和一个整数num,请实现函数将值为num的节点全部删除

【代码1】

时间复杂度O(n),空间复杂度O(n)

class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        ListNode cur = head;
        Stack<ListNode> stack = new Stack<>();
        while(cur != null){
            if(cur.val != val){
                stack.push(cur);
            }
            cur = cur.next;
        }
        while(!stack.isEmpty()){
            stack.peek().next = cur;
            cur = stack.pop();
        }
        return cur;
    }
}

【代码2】

时间复杂度O(n),空间复杂度O(1)

class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        while(head != null){
            if(head.val != val){
                break;
            }
            head = head.next;
        }
        ListNode cur = head;
        ListNode pre = head;
        while(cur != null){
            if(cur.val == val){
                pre.next = cur.next;
            }else{
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会为您介绍如何使用 C 语言实现单链表。首先,我们需要定义一个结构体来表示每个节点: ```c typedef struct Node { int data; // 数据域 struct Node* next; // 指针域 } Node; ``` 接下来,我们需要实现单链表的各种操作: 1. 初始化单链表 ```c void initList(Node** head) { *head = NULL; } ``` 2. 判断单链表是否为空 ```c int isEmpty(Node* head) { return head == NULL; } ``` 3. 获取单链表长度 ```c int getLength(Node* head) { int len = 0; Node* p = head; while (p != NULL) { len++; p = p->next; } return len; } ``` 4. 在单链表末尾插入节点 ```c void insertAtEnd(Node** head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (*head == NULL) { *head = newNode; } else { Node* p = *head; while (p->next != NULL) { p = p->next; } p->next = newNode; } } ``` 5. 在单链表指定位置插入节点 ```c void insertAtPos(Node** head, int pos, int data) { if (pos < 1 || pos > getLength(*head) + 1) { printf("Invalid position!\n"); return; } Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; if (pos == 1) { newNode->next = *head; *head = newNode; } else { Node* p = *head; for (int i = 1; i < pos - 1; i++) { p = p->next; } newNode->next = p->next; p->next = newNode; } } ``` 6. 删除单链表指定位置的节点 ```c void deleteAtPos(Node** head, int pos) { if (pos < 1 || pos > getLength(*head)) { printf("Invalid position!\n"); return; } if (pos == 1) { Node* p = *head; *head = (*head)->next; free(p); } else { Node* p = *head; for (int i = 1; i < pos - 1; i++) { p = p->next; } Node* q = p->next; p->next = q->next; free(q); } } ``` 7. 获取单链表指定位置的节点 ```c int getValueAtPos(Node* head, int pos) { if (pos < 1 || pos > getLength(head)) { printf("Invalid position!\n"); return -1; } Node* p = head; for (int i = 1; i < pos; i++) { p = p->next; } return p->data; } ``` 8. 修改单链表指定位置的节点 ```c void setValueAtPos(Node* head, int pos, int data) { if (pos < 1 || pos > getLength(head)) { printf("Invalid position!\n"); return; } Node* p = head; for (int i = 1; i < pos; i++) { p = p->next; } p->data = data; } ``` 9. 输出单链表 ```c void printList(Node* head) { Node* p = head; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); } ``` 最后,在主函数进行测试: ```c int main() { Node* head; initList(&head); printf("Length: %d\n", getLength(head)); insertAtEnd(&head, 1); insertAtEnd(&head, 2); insertAtEnd(&head, 3); printf("Length: %d\n", getLength(head)); printList(head); insertAtPos(&head, 2, 4); printf("Length: %d\n", getLength(head)); printList(head); deleteAtPos(&head, 3); printf("Length: %d\n", getLength(head)); printList(head); printf("Value at position 2: %d\n", getValueAtPos(head, 2)); setValueAtPos(head, 2, 5); printf("Value at position 2: %d\n", getValueAtPos(head, 2)); printList(head); return 0; } ``` 输出结果为: ``` Length: 0 Length: 3 1 2 3 Length: 4 1 4 2 3 Length: 3 1 4 3 Value at position 2: 4 Value at position 2: 5 1 5 3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值