单链表后续-->练习题

本文探讨了单链表的后续操作,重点解析了一道来自LeetCode的练习题,介绍了一种针对特定条件的优化写法,即在链表中查找某个值并返回其下一个节点。
摘要由CSDN通过智能技术生成

        203. 移除链表元素

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
     ListNode dummyHead=new ListNode();
       dummyHead.next=head;
      ListNode prev=dummyHead;
      while(prev.next!=null){
          if(prev.next.val==val){
              prev.next=prev.next.next;
          }else{
              prev=prev.next;
          }
      }
      return dummyHead.next;
    }
}

递归:

head.val==val ; //return子函数返回值

否则 return  head

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
     if(head==null){
         return null;
     }
     //删除所有值为val的节点后,子链表的头结点可能会发生变化
     //如果子链表的头结点是val,那我就得换个头结点
     //更新当前第二个结点的引用
     head.next=removeElements(head.next,val);
     if(head.val==val){
         return head.next;
     }
     return head;
    }
}

最后If 优化写法

return head.val==val ? head.next :head;

83. 删除排序链表中的重复元素

prev=0,cur=1,

while (cur.val !=null){

cur.val==prev.val ---cur++;

cur.val !=prev.val --  prev.next=cur;prev++, cur++;

}

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
      public ListNode deleteDuplicates(ListNode head) {
            if(head==null||head.next==null){
             return head ;
             }
             ListNode prev=head;
             ListNode cur=prev.next;
              while (cur !=null){
                  if(prev.val!=cur.val){
                      prev.next=cur;
                      prev=prev.next;
                      cur=cur.next;           
                  }else{
                      cur=cur.next;
                  }
              }
             return head;
           }
}

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }
        
        ListNode dummy = new ListNode(0, head);

        ListNode cur = dummy;
        while (cur.next != null && cur.next.next != null) {
            if (cur.next.val == cur.next.next.val) {
                int x = cur.next.val;
                while (cur.next != null && cur.next.val == x) {
                    cur.next = cur.next.next;
                }
            } else {
                cur = cur.next;
            }
        }

        return dummy.next;
    }
}
package  leetCode;

/**
 * 递归方法
 */
class Solution{
    public ListNode deleteDuplicates(ListNode head){
        if (head==null|| head.next==null){
            return head;
        }
        head.next=deleteDuplicates(head.next);
        if (head.val==head.next.val){
            return head.next;
        }else {
            return head;
        }
    }
}
package leetCode;

public class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

82. 删除排序链表中的重复元素 II

package leetCode;

public class num82 {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummyHead = new ListNode();
        dummyHead.next = head;
        ListNode prev = dummyHead;
        ListNode cur = prev.next;
        while (cur != null) {
            ListNode next = cur.next;
            if (next == null) {
                break;
            } else {
                if (cur.val != next.val
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值