设计链表(Java版)

题目链接:LeetCode707.设计链表

在原有代码的基础上加了一个反转链表。

此处只提供了代码,具体讲解可参考代码随想录,此网站上有大佬总结的力扣刷题顺序,个人觉得很有参考意义。

链表结构:

//链表结构
public class ListNode {
	int val;
	ListNode next;
	public ListNode() {}
	public ListNode(int val) {this.val = val;}
	public ListNode(int val, ListNode next) {this.val = val;this.next = next;}
}

设计链表: 

//设计链表
public class MyLinkedList {
	
	int size;//链表长度,不包含虚拟头结点
	ListNode head;//虚拟头结点
	
	public int getSize() {
		return size;
	}

	/**
	 * 初始化
	 */
	public MyLinkedList() {
		size = 0;
		head = new ListNode(0,null);
    }
    
    /** Get the value of the index-th node in the linked list. 
     * If the index is invalid, return -1. 
     */
    public int get(int index) {
    	
    	int res = -1;
    	
    	if(index < 0 || index>=size) {
    		return -1;
    	}
    	ListNode cur = head;
    	//包含一个虚拟头结点,所以查找第index+1个节点
    	for(int i = 0; i<=index; i++) {
    		cur = cur.next;
    	}
    	return cur.val;
    }
    
    /** Add a node of value val before the first element of the linked list. 
     * After the insertion, the new node will be the first node of the linked list. 
     */
    public void addAtHead(int val) {
//    	ListNode newNode = new ListNode(val,head.next);
//    	head.next = newNode;
    	addAtIndex(0, val);
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
    	addAtIndex(size, val);
    }
    
    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
    	if (index>size) {
    		return;
    	}
    	if (index<0) {
    		index = 0;
    	}
    	ListNode pre = head;//待插入位置前驱
    	for(int i = 0; i<index; i++) {
    		pre = pre.next;
    	}
    	ListNode insert = new ListNode(val,pre.next);
    	pre.next=insert;
    	size++;
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
    	if (index<0 || index>=size) {
    		return;
    	}
    	ListNode pre = head;//待删除位置前驱
    	for(int i = 0; i<index; i++) {
    		pre = pre.next;
    	}
    	pre.next = pre.next.next;
    	size--;
    }
    /**
     * 反转链表(新加)
     */
    public void reverseList() {
    	head = this.head;//全局变量head为虚拟结点
		ListNode pre = head;
		ListNode cur = head.next;
		while(cur!=null) {
			ListNode tmp = cur.next;
			cur.next = pre;
			pre = cur;
			cur = tmp;
		}
		head.next = pre;
    }
}

测试: 

//测试
public class ListNodeTest {
	public static void main(String[] args) {
		
		MyLinkedList linkedList = new MyLinkedList();
		int size = linkedList.getSize();
		
		linkedList.addAtHead(1);
		
		linkedList.addAtTail(3);
		
		linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
		size = linkedList.getSize(); 
		System.out.println(size); 
		for (int i = 0; i < size; i++) { 
			System.out.print(linkedList.get(i)+"->"); 
		}
	    System.out.println();
	    System.out.println("=============================");
		linkedList.reverseList();
		size = linkedList.getSize(); 
		System.out.println(size); 
		for (int i = 0; i < size; i++) { 
			System.out.print(linkedList.get(i)+"->"); 
		}
	    System.out.println();
		
		/*
		 * size = linkedList.getSize(); System.out.println(size); for (int i = 0; i <
		 * size; i++) { System.out.print(linkedList.get(i)+"->"); }
		 * System.out.println();
		 * 
		 * System.out.println(linkedList.get(1)); //返回2
		 * System.out.println("===========================");
		 * 
		 * linkedList.deleteAtIndex(1); //现在链表是1-> 3 size = linkedList.getSize();
		 * System.out.println(size); for (int i = 0; i < size; i++) {
		 * System.out.print(linkedList.get(i)+"->"); } System.out.println();
		 * System.out.println(linkedList.get(1)); //返回3
		 */	
		}
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值