1.单链表的初始化
单链表是由节点构成的,所以,在创建单链表时,我们首先应该创建构成单链表的节点
class ListNode{
public int val;
public ListNode next;
public ListNode(value){
this.val = value;
}
}
这里我们设定了节点类,其中val用于存储节点值,next是指向下一个ListNode的引用(c里面称为指针),构造函数初始化对象的值。
然后,我们构造单链表,在单链表中,我们设定一个哨兵节点指向头节点方便我们进行操作,所以在链表类中,我们要初始化哨兵节点。
public class LinkedList{
public LinkedList(){
ListNode dummy = new ListNode(-1);
}
}
这样,我们的初始化就完成了。
后面,我们需要将对应的方法写入链表类中。
2.增加节点
public class LinkedList{
public LinkedList(){
ListNode dummy = new ListNode(-1);
}
public void add(int location, int val){
ListNode pre = dummy; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
pre = pre.next;
}
ListNode node = new ListNode(val);
node.next = pre.next;
pre.next = node;
}
}
3.删除节点
public class LinkedList{
public LinkedList(){
ListNode dummy = new ListNode(-1);
}
public void add(int location, int val){
ListNode pre = dummy; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
pre = pre.next;
}
ListNode node = new ListNode(val);
node.next = pre.next;
pre.next = node;
}
public void remove(int location){
ListNode pre = dummy; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
pre = pre.next;
}
pre.next = pre.next.next;
}
}
这里删除节点我们不考虑location超过链表长度或链表空无法继续删除的情况,以简化操作。
4.查询节点
public class LinkedList{
public LinkedList(){
ListNode dummy = new ListNode(-1);
}
public void add(int location, int val){
ListNode pre = dummy; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
pre = pre.next;
}
ListNode node = new ListNode(val);
node.next = pre.next;
pre.next = node;
}
public void remove(int location){
ListNode pre = dummy; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
pre = pre.next;
}
pre.next = pre.next.next;
}
public int get(int location){
ListNode cur = dummy.next; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
cur = cur.next;
}
return cur.val;
}
}
5.验证是否包含某节点
public class LinkedList{
public LinkedList(){
ListNode dummy = new ListNode(-1);
}
public void add(int location, int val){
ListNode pre = dummy; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
pre = pre.next;
}
ListNode node = new ListNode(val);
node.next = pre.next;
pre.next = node;
}
public void remove(int location){
ListNode pre = dummy; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
pre = pre.next;
}
pre.next = pre.next.next;
}
public int get(int location){
ListNode cur = dummy.next; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
cur = cur.next;
}
return cur.val;
}
public boolean contain(int val){
ListNode head = dummy.next; //初始时指向哨兵节点
while(head != null){
if(head.val = val){
return True;
}
head = head.next;
}
return False;
}
}
6.设置打印链表的函数
public class LinkedList{
public LinkedList(){
ListNode dummy = new ListNode(-1);
}
public void add(int location, int val){
ListNode pre = dummy; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
pre = pre.next;
}
ListNode node = new ListNode(val);
node.next = pre.next;
pre.next = node;
}
public void remove(int location){
ListNode pre = dummy; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
pre = pre.next;
}
pre.next = pre.next.next;
}
public int get(int location){
ListNode cur = dummy.next; //初始时指向哨兵节点
for(int i = 0; i < location; i++){
cur = cur.next;
}
return cur.val;
}
public boolean contain(int val){
ListNode head = dummy.next; //初始时指向哨兵节点
while(head != null){
if(head.val = val){
return True;
}
head = head.next;
}
return False;
}
public void print(){
ListNode head = dummy.next;
while(head != null){
System.out.print(head.val + "->");
head = head.next;
}
System.out.println("null");
}
}