赞
踩
题目描述:
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
示例:
MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
linkedList.get(1); //返回2
linkedList.deleteAtIndex(1); //现在链表是1-> 3
linkedList.get(1); //返回3
分析:
题目描述中,在设计链表的时候,可以考虑单链表和双链表两种情况,而单链表和双链表唯一的区别就是双链表比单链表在节点上多了一个指向前的引用。因此,我们在这分两种情况做这个题。
思路一:单链表
代码:
class ListNode{ public int val; public ListNode next; public ListNode(int val) { this.val=val; this.next=null; } } class MyLinkedList { private int size; public ListNode head; /** Initialize your data structure here. */ public MyLinkedList() { this.size=0; this.head=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) { ListNode cur=head; while(cur!=null) { if(index==0) { return cur.val; } index--; cur=cur.next; } return -1; } /** 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 cur= new ListNode(val); cur.next=head; head=cur; this.size++; } /** Append a node of value val to the last element of the linked list. */ public void addAtTail(int val) { if(size==0) { head=new ListNode(val); return; } ListNode cur=head; while(cur.next!=null) { cur=cur.next; } cur.next=new ListNode(val); this.size++; } /** 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<=0) { addAtHead(val); return; } int num=0; ListNode cur=head; while(cur!=null) { if(num==index-1) { ListNode Ttmp=cur.next!=null?cur.next:null; cur.next=new ListNode(val); cur.next.next=Ttmp; size++; return; } num++; cur=cur.next; } return ; } /** 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; } if(index==0) { head=head.next; size--; return; } ListNode cur=head; size--; for(int i=0;i<index-1;i++) { cur=cur.next; } cur.next=cur.next.next; } }
思路二:双向链表
对于双向链表,他唯一不同的就是多了一个指向前的引用prev,在实现各个方法的过程中,一定要注意不要忘记改变prev的指向,还有就是当改变prev指向的同时尤其得判断一下这个节点是否为空,为空的话就不能改变他的prev指向了。
代码:
class ListNode{ public int val; public ListNode next; public ListNode prev; public ListNode(int val) { this.val=val; this.next=null; this.prev=null; } } class MyLinkedList { private int size; public ListNode head; /** Initialize your data structure here. */ public MyLinkedList() { this.size=0; this.head=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) { ListNode cur=head; while(cur!=null) { if(index==0) { return cur.val; } index--; cur=cur.next; } return -1; } /** 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 cur= new ListNode(val); cur.next=head; cur.prev=null; head=cur; this.size++; } /** Append a node of value val to the last element of the linked list. */ public void addAtTail(int val) { if(size==0) { head=new ListNode(val); return; } ListNode cur=head; while(cur.next!=null) { cur=cur.next; } cur.next=new ListNode(val); cur.next.prev=cur; this.size++; } /** 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<=0) { addAtHead(val); return; } int num=0; ListNode cur=head; while(cur!=null) { if(num==index-1) { ListNode Ttmp=cur.next!=null?cur.next:null; cur.next=new ListNode(val); cur.next.next=Ttmp; cur.next.prev=cur; if(Ttmp!=null) Ttmp.prev=cur.next; size++; return; } num++; cur=cur.next; } return ; } /** 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; } if(index==0) { head=head.next; size--; return; } ListNode cur=head; size--; for(int i=0;i<index-1;i++) { cur=cur.next; } cur.next=cur.next.next; if(cur.next!=null) cur.next.prev=cur; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。