链表常规应用方式(Java)

// ListNode结构
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; }
}
	**// 在ListNode尾部添加一个新节点并赋值val**
    public boolean addOne(ListNode head, int val) {  
        ListNode newNode = new ListNode(val);	// 实例化一个新节点val
	    if (head == null) {						// 若头节点为空,新节点赋值给头节点
            head = newNode;
            return true;
        }
        ListNode tmp = head;  		// 若头节点不为空,用tmp代替head进行操作。防止head的值被修改
        while (tmp.next != null) { 	// 遍历到最后一个节点
            tmp= tmp.next;
        }
        tmp.next = newNode ; 		// 让上一个节点的next指向新节点,完成连接
        return true;
    }
	**// 将节点插入指定位置**
    public void insertListNode(ListNode head, ListNode node, int pos){
        if(head == null){
            head = new ListNode();
        }
        ListNode tmp = head;
        int count = 0;
        while(tmp.next != null){
            if(pos == count){
               node.next = tmp.next;
               tmp.next = node;
               return;
            }
            count++;
        }
        tmp.next = node;
    }
  **//删除某一指定值为val的节点**
  public boolean deleteOne(ListNode head, int val){ 
      if(head == null){  			// 若头节点为空,返回false
          return false; 
      }
      if(head.val == val){ 		// 头结点为目标值,直接将下一节点覆盖即可
          head = head.next;
          return true;
      }
      ListNode tmp = head;
      while (tmp.next != null){ 	// 遍历所有节点
      	//对tmp.next节点进行判断,如果是要删除的,直接让tmp.next指向被删除节点的next节点。
      	//即为:tmp.next = tmp.next.next;
          if(tmp.next.val == val){
              tmp.next = tmp.next.next;
              return true;
          }
          tmp = tmp.next;
      }
      return false;
  }
	**//计算ListNode节点长度**
    public int getLength(ListNode head){  
        int len = 0;
        ListNode tmp = head;
        while(tmp != null){
            len++;
            tmp = tmp.next;
        }
        return len;
    }
  	**//查找某一特定节点并返回其下标**
    public int findNode(ListNode head, int val){
        ListNode tmp = head;
        int index = 0;
        while(tmp.next != null){
            if(tmp.val != val){
                index++;
                tmp = tmp.next;
            }
            return index;
        }
        return -1;
    }
 	**//根据下标查找指定节点**
    public ListNode getNode(ListNode head, int index){
        if(head == null){
            return null;
        }
        ListNode tmp = head;
        for (int i=0; i<index; i++) {
        	if (tmp.next == null) {
        		return null;
        	}
            tmp = tmp.next;
        }
        return tmp;
    }
  **//链表反转**
  public ListNode reverse(ListNode head){
  	ListNode tmp = head;
      ListNode result = null;
      if(head == null){
          return null;
      }
      while(tmp != null) {
          ListNode ln = tmp.next;
          tmp.next = result;
          result = tmp;
          tmp= ln;
      }
      return result;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值