本文共 698 字,大约阅读时间需要 2 分钟。
删除链表中指定值的node
非递归:
public class Solution { public ListNode removeElements(ListNode head, int target) { ListNode pre = new ListNode(0); pre.next = head; ListNode cur = pre; while (cur != null && cur.next != null) { while (cur != null && cur.next != null && cur.next.val == target) { cur.next = cur.next.next; } cur = cur.next; } return pre.next; }}
递归:
public 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; }}
转载地址:http://bthld.baihongyu.com/