移除链表元素

题目描述:给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-linked-list-elements/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:链表遍历

首先,初始化一个结点firstNode指向head结点,cur指向head结点,last指向firstNode结点,然后开始遍历:

  • 首先cur不能为空;
  • 如果cur结点的值等于目标值val,则将last的next指向cur的next,并且cur赋值为cur的next;
  • 如果cur结点的值不等于目标值val,则将last和cur结点往后移一位。

遍历结束后,返回firstNode的next结点即为处理后的链表。

  1. public class LeetCode_203 {
  2. public static ListNode removeElements(ListNode head, int val) {
  3. ListNode firstNode = new ListNode(-1);
  4. firstNode.next = head;
  5. ListNode cur = firstNode.next;
  6. ListNode last = firstNode;
  7. while (cur != null) {
  8. if (cur.val == val) {
  9. last.next = cur.next;
  10. cur = last.next;
  11. } else {
  12. last = cur;
  13. cur = cur.next;
  14. }
  15. }
  16. return firstNode.next;
  17. }
  18. public static void main(String[] args) {
  19. ListNode head = new ListNode(1);
  20. head.next = new ListNode(2);
  21. head.next.next = new ListNode(6);
  22. head.next.next.next = new ListNode(3);
  23. head.next.next.next.next = new ListNode(6);
  24. removeElements(head, 6);
  25. while (head != null) {
  26. System.out.print(head.val + " ");
  27. head = head.next;
  28. }
  29. }
  30. }

【每日寄语】 在这个并非尽善尽美的世界上,勤奋会得到报偿,而游手好闲则要受到惩罚。