TreeviewCopyright © aleen42 all right reserved, powered by aleen42

203. 移除链表元素

https://leetcode-cn.com/problems/remove-linked-list-elements/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-07-16 16:59:54
 * @Description: https://leetcode-cn.com/problems/remove-linked-list-elements/
 * @FilePath: \leetcode-googtech\#203. Remove Linked List Elements\Solution.java
 */ 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        // 判断头节点是否为空
        if(head==null){
            return null;
        }

        // 初始化辅助节点
        ListNode newNode = new ListNode(0);
        newNode.next = head;

        // 初始化当前节点与前继节点指针 
        ListNode previousNode = newNode;
        ListNode currentNode = head; 

        // 遍历寻找待删除节点
        while(currentNode!=null){
            if(currentNode.val == val){
                previousNode.next = currentNode.next;
            }else{
                previousNode = currentNode;
            }
            currentNode = currentNode.next;
        }
        // 返回头节点
        return newNode.next;
    }
}

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

Example:
Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
*/

Python

'''
@Author: Goog Tech
@Date: 2020-07-16 17:00:01
@Description: https://leetcode-cn.com/problems/remove-linked-list-elements/
@FilePath: \leetcode-googtech\#203. Remove Linked List Elements\Solution.py
'''
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        # 初始化辅助节点
        newNode = ListNode(0)
        newNode.next = head

        # 初始化当前节点与前继节点指针
        previousNode,currentNode = newNode,head

        # 遍历寻找待删除节点
        while currentNode:
            if currentNode.val == val:
                previousNode.next = currentNode.next
            else:
                previousNode = currentNode
            currentNode = currentNode.next
        # 返回头节点: same as return head
        return newNode.next

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

Example:
Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
"""
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""