首页 > 学院 > 开发设计 > 正文

[LeetCode] Remove Linked List Elements

2019-11-15 01:06:04
字体:
来源:转载
供稿:网友
[LeetCode] Remove Linked List Elements

Remove all elements from a linked list of integers that have valueval.

ExampleGiven:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,val= 6Return:1 --> 2 --> 3 --> 4 --> 5

这道题两个指针就可以了哇。不过我总是把指针搞混。所以我还是觉得有点绕T T

public class Solution {    public ListNode removeElements(ListNode head, int val) {        ListNode track = new ListNode(0);          track.next = head;          ListNode a = track;          ListNode b = head;          while(b!=null) {              if(b.val == val) {                  a.next = b.next;              } else {                  a = a.next;              }              b = b.next;          }                    return track.next;      }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表