List链表(Basic)

  1 //如果数据结构都还没搞明白,就先别刷力扣.先敲一下基本数据结构  2 
  3 /**
  4  * @Auther: Caojc
  5  * @Date: 2022/3/17 - 03 - 17 - 19:50
  6  * @Description: 链表,画出来就很简单.注意考虑边界条件
  7  * @version: 1.0
  8  */
  9 public class createListNode {
 10     //自定义链表
 11     static class ListNode{
 12         int val;
 13         ListNode next;
 14 
 15         public ListNode() {
 16         }
 17 
 18         public ListNode(int val) {
 19             this.val = val;
 20         }
 21 
 22         public ListNode(int val, ListNode next) {
 23             this.val = val;
 24             this.next = next;
 25         }
 26     }
 27     //根据数组创建链表
 28     public static ListNode createList(int[] array){
 29         ListNode head=null;
 30         ListNode pre=null;
 31         ListNode curr=null;
 32         for (int i = 0; i <array.length-1 ; i++) {
 33             curr=new ListNode(array[i]);
 34             if(i==0){
 35                 head=curr;
 36             }else{
 37                 pre.next=curr;
 38             }
 39             pre=curr;
 40         }
 41         return head;
 42     }
 43     //打印链表
 44     public static void printList(ListNode head){
 45         while(head!=null){
 46             System.out.print(head.val+"->");
 47             head=head.next;
 48         }
 49         System.out.println("null");
 50     }
 51     //按照节点值删除链表
 52     public static ListNode deleteList(ListNode head,int val){
 53         if(head.val==val) return head.next;
 54         ListNode curr=head;
 55         while(curr!=null&&curr.val!=val){
 56             curr=curr.next;
 57         }
 58         curr.next=curr.next.next;
 59         return head;
 60 
 61     }
 62     //获取链表长度
 63     public static int getListLength(ListNode head){
 64         int len=0;
 65         while(head!=null){
 66             head=head.next;
 67             len++;
 68         }
 69         return len;
 70     }
 71     //删除第index个节点
 72     public static ListNode indexDeleteList(ListNode head,int index){
 73         ListNode curr=head;
 74         int len=getListLength(head);
 75         try {
 76             if (index<0||index > len) throw new IndexOutOfBoundsException();
 77             if(index==1) return head.next;
 78             while(index-2>0){
 79                 curr=curr.next;
 80                 index--;
 81             }
 82             curr.next=curr.next.next;
 83         }catch (IndexOutOfBoundsException e){
 84             System.out.println("超出链表长度");
 85             e.printStackTrace();
 86         }
 87         return head;
 88     }
 89     //删除链表倒数第index个节点
 90     public static ListNode reverseIndexDeleteListNode(ListNode head,int index){
 91         ListNode curr=head;
 92         int len=getListLength(head);
 93         try {
 94             if (index<0||index > len) throw new IndexOutOfBoundsException();
 95             if(index==len) return head.next;
 96             //找到正数len-index个节点前一个节点
 97             for (int i = 0; i <len-index-1 ; i++) {
 98                 curr=curr.next;
 99             }
100             curr.next=curr.next.next;
101         }catch (IndexOutOfBoundsException e){
102             System.out.println("超出链表长度");
103             e.printStackTrace();
104         }
105         return head;
106 
107     }
108     //反转链表
109     public static ListNode reverseList(ListNode head){
110         ListNode pre=null;
111         ListNode curr=head;
112         while(curr!=null){
113             ListNode next=curr.next;
114             curr.next=pre;
115             pre=curr;
116             curr=next;
117         }
118         return pre;
119     }
120     //反转L位置和R位置之间的链表
121     public static ListNode reversePartList(ListNode head,int L,int R){
122         ListNode dummyNode=new ListNode(0,head);
123         ListNode LNode=null;
124         ListNode RNode=null;
125         ListNode begin=null;
126         ListNode end=null;
127         ListNode start=dummyNode;
128         try {
129             if (L > R || R < 0 || L > getListLength(head)) throw new RuntimeException("起始位置异常");
130             for (int i = 0; i < L - 1; i++) {
131                 start = start.next;
132             }
133             begin = start;
134             LNode = begin.next;
135             for (int i = 0; i < R - L + 1; i++) {
136                 start = start.next;
137             }
138             RNode = start;
139             end = RNode.next;
140             begin.next=null;
141             RNode.next=null;
142             reverseList(head);
143             begin.next = RNode;
144             LNode.next=end;
145         } catch (RuntimeException e) {
146             e.printStackTrace();
147         }
148 
149         return dummyNode.next;
150     }
151     //主函数测试
152     public static void main(String[] args) {
153         int[] array={3,4,51,34,32,5,5,32};
154         ListNode head=createList(array);
155         System.out.println("创建链表");
156         printList(head);
157         System.out.println("删除指定值的链表元素");
158         deleteList(head,5);
159         printList(head);
160         System.out.println("反转链表");
161         head=reverseList(createList(array));
162         printList(head);
163         System.out.println("删除指定位置的链表节点");
164         printList(indexDeleteList(createList(array),1));
165         System.out.println("删除倒数第n个节点");
166         printList(reverseIndexDeleteListNode(createList(array),getListLength(createList(array))));
167         printList(reverseIndexDeleteListNode(createList(array),1));
168         System.out.println("反转L位置和R位置之间的链表");
169         ListNode a = createList(array);
170         printList(reversePartList(a, 1, 7));
171 
172     }
173 }

 

posted @ 2022-03-18 19:58  实力会让你自信  阅读(63)  评论(0)    收藏  举报