链表定义和常用操作
在刷关于链表的题时,因为想在本地的idea里测,需要重新构造一些链表的方法,先对链表进行定义,是由data和next组成。如果是双链表就加一个pre节点。如下:
public class ListNode implements MyList{
Object data;
ListNode next;
ListNode pre;
public ListNode(Object data) {
this.data = data;
}
在Util类中加一些链表的基本操作:先定义一个接口myList,接口中方法为常见的基本操作,可以方便其他的类实现(java多态):
public interface MyList extends Iterator {
void add(Object element);
void delete(Object element);
void delete(int index);
void update(int index, Object newElement);
boolean contains(Object target);
Object select(int index);
}
在ListNode中,可以先添加一个add方法和重写toString方法,因为这两个方法太常用:
public class ListNode{
public Object data;
public ListNode next;
public ListNode pre;
public ListNode(Object data) {
this.data = data;
}
public void add(Object element) {
ListNode node = new ListNode(element);
ListNode cur = this;
while (cur.next!= null){
cur = cur.next;
}
cur.next = node;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
ListNode p = this;
while (p!= null){
sb.append(p.data);
if (p.next != null){
sb.append("->");
}
p = p.next;
}
sb.append("]");
return sb.toString();
}
}
测试
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.add(2);
head.add(3);
head.add(2);
head.add(1);
System.out.println(head);
)
[1->2->3->2->1]
Process finished with exit code 0
对myList中方法的实现,相当于写一个LinkedList类。如下:
package 链表;
public class SingleLinkedList implements MyList{
private ListNode head;
private ListNode last;
private int size;
@Override
public void add(Object element) {
if (head == null){
head = new ListNode(element);
last = head;
}else {
last.next = new ListNode(element);
last = last.next;
}
size++;
}
@Override
public void delete(Object element) {
ListNode p = head;
ListNode pre = null;
while (p != null){
if (p.data.equals(element)){
if (p == head){
head = head.next;
}else {
pre.next = p.next;
}
size--;
break;
}
pre = p;
p = p.next;
}
}
@Override
public void delete(int index) {
if (index < 0 || index >= size) return;
int i = 0;
ListNode p = head;
ListNode pre = null;
while (p != null){
if (i == index){
if (p == head){
head = head.next;
}else {
pre.next = p.next;
}
size--;
break;
}
pre = p;
p = p.next;
i++;
}
}
@Override
public void update(int index, Object newElement) {
if (index < 0 || index >= size) return;
int i = 0;
ListNode p = head;
while (p != null){
if (i == index){
p.data = newElement;
break;
}
p = p.next;
i++;
}
}
@Override
public boolean contains(Object target) {
ListNode p = head;
while (p != null){
if (p.data.equals(target)){
return true;
}
p = p.next;
}
return false;
}
@Override
public Object select(int index) {
if (index < 0 || index >= size) return null;
int i = 0;
ListNode p = head;
while (p != null){
if (i == index){
return p.data;
}
p = p.next;
i++;
}
return null;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
ListNode p = head;
while (p!= null){
sb.append(p.data);
if (p.next != null){
sb.append(",");
}
p = p.next;
}
sb.append("]");
return sb.toString();
}
@Override
public boolean hasNext() {
return false;
}
@Override
public Object next() {
return null;
}
}
链表常见问题: 反转链表、回文链表、合并链表…放到下一个文章中吧。
|