203. Remove Linked List Elements

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

Example
Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
Return: 1 –> 2 –> 3 –> 4 –> 5

给出一条单链表,删除指定值的节点,返回更新后的链表

两种解法:

1.设定一个指向头节点的node节点,再让head节点从这个前导节点开始遍历

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* temp;
        temp->next = head;    //设定一个前导指针
        head = temp;          //head现在指向它的前一个指针
        while(head->next != NULL){
            if(head->next->val == val){
                head->next = head->next->next;
            }
            else{
                head = head->next;
            }
        }       
        return temp->next;    
    }
};

python:

class Solution(object):
    def removeElements(self, head, val):
        temp = ListNode(0)
        temp.next = head
        head = temp

        while head.next != None:
            if head.next.val == val:
                head.next = head.next.next
            else:
                head = head.next
        return temp.next      

2.逐项考虑情况,主要考虑队头

1.从头部开始的时候如果遇到相等的,直接就可以将head往更新为下一个元素。直到第一个不相等,或者NULL时,跳出循环
2.如果是为NULL就不进入循环,如果是第一个不等元素,直接判断下一个是否相等,相等temp.next = temp.next.next跳过这个相等的元素,否则temp = temp->next;继续下一个判断。

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {


        while(head != NULL && head->val == val){
            head = head->next;
        }
        //这里把前面的相等元素全剔除掉了,这时候的head是第一个不等于val值的,或者是为NULL
        ListNode* temp = head;
        while(temp != NULL && temp->next != NULL){
            if(temp->next->val == val){
                temp->next = temp->next->next;
            }
            else{
                temp = temp->next;
            }   
        }
        return head;
    }
};

python:

class Solution(object):
    def removeElements(self, head, val):
        while head != None and head.val == val:
                head = head.next

        temp = ListNode(0)
        temp = head
        while temp != None and temp.next != None:
            if temp.next.val == val:
                temp.next = temp.next.next
            else:
                temp = temp.next
        return 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、付费专栏及课程。

余额充值