203. Remove Linked List Elements

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目要求
Remove all elements from a linked list of integers that have value val.
这里写图片描述
删除给定链表中对应值的节点。

2,题目思路
对于这样的一个问题,只要依次对链表进行遍历即可,设置两个节点来进行跟踪,一个节点cur来读取目前遍历的节点,另一个用来记录上一个节点。
需要注意的是,对于第一个符合条件的节点,要单独的进行判断处理。
对于这样的问题,还可以用递归的办法来解决。

3,程序源码
方法1:

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* pre = NULL;
        ListNode* cur = head;
        while(cur!= NULL)
        {
            if(cur->val == val) {
                if (pre == NULL) {        //第一个节点
                    head = head->next;
                }
                else {
                    pre->next = cur->next;
                }
            }

            else {
                pre = cur;
            }

            cur = cur->next;
        }

        return head;
    }
};

方法2:

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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here's an example of Java code to implement a doubly circular linked list: ```java public class DoublyCircularLinkedList { private Node head; private int size; private class Node { private int data; private Node previous; private Node next; public Node(int data) { this.data = data; } } // Constructor public DoublyCircularLinkedList() { head = null; size = 0; } // Check if the list is empty public boolean isEmpty() { return head == null; } // Get the size of the list public int getSize() { return size; } // Add an element to the beginning of the list public void addFirst(int data) { Node newNode = new Node(data); if (isEmpty()) { newNode.next = newNode; newNode.previous = newNode; head = newNode; } else { newNode.next = head; newNode.previous = head.previous; head.previous.next = newNode; head.previous = newNode; head = newNode; } size++; } // Add an element to the end of the list public void addLast(int data) { Node newNode = new Node(data); if (isEmpty()) { newNode.next = newNode; newNode.previous = newNode; head = newNode; } else { newNode.next = head; newNode.previous = head.previous; head.previous.next = newNode; head.previous = newNode; } size++; } // Remove the first element from the list public void removeFirst() { if (isEmpty()) { System.out.println("List is empty!"); return; } if (size == 1) { head = null; } else { head.next.previous = head.previous; head.previous.next = head.next; head = head.next; } size--; } // Remove the last element from the list public void removeLast() { if (isEmpty()) { System.out.println("List is empty!"); return; } if (size == 1) { head = null; } else { head.previous.previous.next = head; head.previous = head.previous.previous; } size--; } // Display the elements of the list public void display() { if (isEmpty()) { System.out.println("List is empty!"); return; } Node current = head; do { System.out.print(current.data + " "); current = current.next; } while (current != head); System.out.println(); } // Main method to test the implementation public static void main(String[] args) { DoublyCircularLinkedList list = new DoublyCircularLinkedList(); list.addFirst(3); list.addFirst(2); list.addFirst(1); list.addLast(4); list.addLast(5); System.out.println("Elements of the list: "); list.display(); list.removeFirst(); list.removeLast(); System.out.println("Elements after removing first and last: "); list.display(); } } ``` This code creates a DoublyCircularLinkedList class with methods to add elements to the beginning and end, remove elements from the beginning and end, check if the list is empty, get the size of the list, and display the elements of the list. The main method is used to test the implementation by adding elements and then removing the first and last elements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值