Remove Linked List Elements
Description
Remove all elements from a linked list of integers that have value val.
Example 1:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
Tags: Linked List
解读题意
移除链表中指定的元素
思路1
- 定义一个新的节点newHead作为头元素,并且
newHead.next = head
,定义pre(用来保留上一个节点)指向新的头元素newHead,current(当前节点)指向head - 如果
current.val == val
,就移除当前元素,即current;否则pre指向当前元素 - 当前元素current后移,
current = current.next
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode newHead = new ListNode(val - 1);
newHead.next = head;
ListNode pre = newHead;
ListNode current = head;
while (current != null) {
if (current.val == val)
pre.next = current.next;
else
pre = current;
current = current.next;
}
return newHead.next;
}
}
分析:
- Time complexity:O(n)
leetCode汇总:https://blog.csdn.net/qingtian_1993/article/details/80588941
项目源码,欢迎star:https://github.com/mcrwayfun/java-leet-code