查询节点并删除

    xiaoxiao2021-12-12  2

    package 链表中; import com.jikexueyuan.listNode.ListNode; /** * 查询节点并删除 */ public class RemoveLinkedListElements { public ListNode removeElements(ListNode head, int val) { if (head == null) { return head; } else { ListNode newHead = new ListNode(0); newHead.next = head; ListNode pre = newHead; ListNode p = head; while (p != null) { if (p.val == val) { pre.next = p.next; p = p.next; } else { pre = p; p = p.next; } } return newHead.next; } } @Test public void test() { int[] array1 = {1, 6, 6, 4, 6, 3, 5}; ListNode head = ListNode.arrayToList(array1); head = removeElements(head, 6); ListNode.printList(head); int[] array2 = {1, 2, 3, 4, 1}; head = ListNode.arrayToList(array2); head = removeElements(head, 1); ListNode.printList(head); } }
    转载请注明原文地址: https://ju.6miu.com/read-900122.html

    最新回复(0)