LeetCode203 707链表

- 双向链表的使用
- 增加虚拟头节点方便操作
This commit is contained in:
whai 2023-12-27 19:44:46 +08:00
parent 9385b628ad
commit 8ad2f18126

View File

@ -174,13 +174,38 @@ public class LeetCode203 {
}
/**
* 0 1 2 3
* 加上虚拟表头节点
* @param head
* @param val
* @return
*/
public ListNode removeElements7(ListNode head, int val) {
ListNode virtualHead = new ListNode(0, head);
ListNode pre = virtualHead;
ListNode index = head;
head = virtualHead;
while (index != null) {
if (index.val == val) {
pre.next = index.next;
index = pre.next;
}else {
pre = pre.next;
index = index.next;
}
}
return head.next;
}
@Test
@Test
public void Test() {
ListNode listNode = ListNode.listNodeFromArray(new int[]{6,6,6,6});
ListNode listNode = ListNode.listNodeFromArray(new int[]{7,7,7,7});
// ListNode.printList(listNode);
ListNode listNode1 = removeElements(listNode, 6);
ListNode listNode1 = removeElements7(listNode, 7);
ListNode.printList(listNode1);
}
}