标签:direct 变量 数据 href node list lan 图片 div
链表和递归
5-1 Leetcode中和链表相关的问题
Java类的递归,包含的成员变量有该类本身。
ListNode
//Definition for singly-linked list. public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; } }
/// Leetcode 203. Remove Linked List Elements
/// https://leetcode.com/problems/remove-linked-list-elements/description/
Solution
/// Leetcode 203. Remove Linked List Elements /// https://leetcode.com/problems/remove-linked-list-elements/description/ class Solution { public ListNode removeElements(ListNode head, int val) { while(head != null && head.val == val){ ListNode delNode = head; head = head.next; delNode.next = null; } if(head == null) return head; ListNode prev = head; while(prev.next != null){ if(prev.next.val == val) { ListNode delNode = prev.next; prev.next = delNode.next; delNode.next = null; } else prev = prev.next; } return head; } }
标签:direct 变量 数据 href node list lan 图片 div
原文地址:https://www.cnblogs.com/MarlonKang/p/12344067.html