zoukankan      html  css  js  c++  java
  • 5-2******* 测试自己的Leetcode链表代码

     1 class Solution3 {
     2 
     3     public ListNode removeElements(ListNode head, int val) {
     4 
     5         ListNode dummyHead = new ListNode(-1);
     6         dummyHead.next = head;
     7 
     8         ListNode prev = dummyHead;
     9         while(prev.next != null){
    10             if(prev.next.val == val)
    11                 prev.next = prev.next.next;
    12             else
    13                 prev = prev.next;
    14         }
    15 
    16         return dummyHead.next;
    17     }

    实际在编程的时候免不了要进行调试,就要用到main函数。

    对于solution3,可以为它写一个main函数,调用removeElements方法,就需要创建一个由ListNode组成的链表,但是对于我们现在的代码是没有链表创建功能的,所以根本不可能进行调试,在这里同学们完全可以自己写一个函数来创建链表,简单举一个例子。

    比如说链表中所有节点包含的元素是

    1, 2, 6, 3, 4, 5, 6  

    可以声明数组

    int[] nums = {1, 2, 6, 3, 4, 5, 6};   //声明一个数组是int[]类型的

    下面需要一个方法把数组转成一个链表,对此,把方法写在ListNode类中
     1 //Definition for singly-linked list.
     2 public class ListNode {
     3 
     4     public int val;
     5     public ListNode next;
     6 
     7     public ListNode(int x) {
     8         val = x;
     9     }//构造函数,传入一个数值。
    10 
    11     // 链表节点的构造函数 ,把数组转成一个链表
    12     // 使用arr为参数,创建一个链表,当前的ListNode为链表头结点
    13     public ListNode(int[] arr){
    14 
    15         if(arr == null || arr.length == 0)
    16             throw new IllegalArgumentException("arr can not be empty");
    17 
    18         this.val = arr[0];   // this.val对应arr中索引为0的元素
    19         //遍历整个数组,在这个过程中,我们一个一个的将数组中的每一个元素创建成新的ListNode,接在前一个节点
    20         ListNode cur = this;  //从节点ListNode(arr[0])开始
    21         for(int i = 1 ; i < arr.length ; i ++){
    22             cur.next = new ListNode(arr[i]);
    23             cur = cur.next;     //最后我们的这个this其实是:用for循环创建出来的链表相对应的头节点
    24         }
    25     }
    26 
    27     // 以当前节点为头结点的链表信息字符串
    28     @Override  //为了方便在main函数中看到链表是什么
    29     public String toString(){
    30 
    31         StringBuilder s = new StringBuilder();
    32         ListNode cur = this;
    33         while(cur != null){
    34             s.append(cur.val + "->");
    35             cur = cur.next;
    36         }
    37         s.append("NULL");
    38         return s.toString();
    39     }
    40 }

    接着可以在solution→main函数中调用这个方法

     
     1 class Solution3 {
     2 
     3     public ListNode removeElements(ListNode head, int val) {
     4 
     5         ListNode dummyHead = new ListNode(-1);
     6         dummyHead.next = head;
     7 
     8         ListNode prev = dummyHead;
     9         while(prev.next != null){
    10             if(prev.next.val == val)
    11                 prev.next = prev.next.next;
    12             else
    13                 prev = prev.next;
    14         }
    15 
    16         return dummyHead.next;
    17     }
    18 //**************添加main函数**************************//
    19     public static void main(String[] args) {
    20 
    21         int[] nums = {1, 2, 6, 3, 4, 5, 6};   //声明一个数组是int[]类型的
    22         ListNode head = new ListNode(nums); // 调用ListNode方法 ,这里的head是一个链表,由于构造函数public ListNode(int[] arr)
    23         System.out.println(head);//这里的head虽然是一个头节点,但是根据我们在头结点中覆盖的toString()方法,将打印以head为头节点的整个链表对应的字符串
    24         //测试,调用Solution3
    25         ListNode res = (new Solution3()).removeElements(head, 6);
    26         System.out.println(res);
    27     }
    28 }
    
    
    
    
    
    
    
     
    带女朋友搬家新家条件不好,累到女朋友了,让女朋友受苦了,特此明志:每天学习,明年这个时候(20190812)让女朋友住上大房子,永远年轻,永远热泪盈眶,很多人都是这样,他们都把自己当成身在梦中一样,浑浑噩噩地过日子,只有痛苦或爱或危险可以让他们重新感到这个世界的真实。
  • 相关阅读:
    使用 cordova-plugin-wechat 分享返回后闪退解决方法
    恢复删除的表
    移动端还原设计图
    js文本差异比较
    windows使用nvm安装nodejs后升级npm报错
    windows添加右键菜单"在此处打开cmd窗口"
    cordova热更新
    js变量提升
    c# 判断字符串是否是日期格式需要注意的一点小问题
    剑指offer_和为S的两个数字
  • 原文地址:https://www.cnblogs.com/make-big-money/p/10326665.html
Copyright © 2011-2022 走看看