[LeetCode-JAVA] Remove Linked List Elements
生活随笔
收集整理的這篇文章主要介紹了
[LeetCode-JAVA] Remove Linked List Elements
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目:
Remove all elements from a linked list of integers that have value?val.
Example
Given:?1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,?val?= 6
Return:?1 --> 2 --> 3 --> 4 --> 5
思路:設(shè)置前置指針,并隨之移動。
代碼:
public class Solution {public ListNode removeElements(ListNode head, int val) {ListNode req = new ListNode(0);req.next = head;ListNode pre = req;while(head != null){if(head.val == val){pre.next = head.next;}elsepre = pre.next;head = head.next;}return req.next;} }?
轉(zhuǎn)載于:https://www.cnblogs.com/TinyBobo/p/4543536.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的[LeetCode-JAVA] Remove Linked List Elements的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深信服桌面云的各种密码
- 下一篇: Apache日志配置