zoukankan      html  css  js  c++  java
  • 《程序员代码面试指南》第二章 链表问题 在单链表中删除指定值的节点

    样例

    1 2 3 4 5 6 7 8 9 10 ,num=3 ,结果:1 2 4 5 6 7 8 9 10
    

    java代码

    /**
     * @Description:在单链表中删除指定值的节点
     * @Author: lizhouwei
     * @CreateDate: 2018/4/7 10:33
     * @Modify by:
     * @ModifyDate:
    */
    public class Chapter2_14 {
        public Node removeNode(Node head, int num) {
            if (head == null) {
                return null;
            }
            //当要删除的节点头部连续的几个的话,则都删除
            while (head != null && head.vlaue == num) {
                head = head.next;
            }
            Node pre = head;
            Node cur = head.next;
            while (cur != null) {
                if (cur.vlaue == num) {
                    pre.next = cur.next;
                } else {
                    pre = cur;
                }
                cur = cur.next;
            }
            return head;
        }
    
        //测试
        public static void main(String[] args) {
            Chapter2_14 chapter = new Chapter2_14();
            Link link = new Link();
            //构造链表
            for (int i = 10; i > 0; i--) {
                link.add(i);
            }
            Link.printLink(link.head);
            Node head = chapter.removeNode(link.head, 3);
            Link.printLink(head);
        }
    }
    
  • 相关阅读:
    1029: [JSOI2007]建筑抢修
    1028: [JSOI2007]麻将
    1050 棋盘染色 2
    1026: [SCOI2009]windy数
    1074: [SCOI2007]折纸origami
    839. Optimal Marks
    1024: [SCOI2009]生日快乐
    1025: [SCOI2009]游戏
    1023: [SHOI2008]cactus仙人掌图
    对前面的总结
  • 原文地址:https://www.cnblogs.com/lizhouwei/p/8732554.html
Copyright © 2011-2022 走看看