玩转数据结构:第5章 链表和递归
链表和递归
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; } }
相关推荐
koushr 2020-11-12
范范 2020-10-28
qiangde 2020-09-13
范范 2020-07-30
mingyunxiaohai 2020-07-19
aanndd 2020-08-12
zhaochen00 2020-10-13
Mars的自语 2020-09-27
steeven 2020-09-18
kka 2020-09-14
聚沙成塔积水成渊 2020-08-16
earthhouge 2020-08-15
bluetears 2020-07-28
horizonheart 2020-07-19
liushall 2020-07-18
bluetears 2020-07-05
fengshantao 2020-07-02
liuweixiao0 2020-06-27