数据结构知识点 -- 链表(Java实现)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                数据结构知识点 -- 链表(Java实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                持續更新
文章目錄
- 簡單題
- 1. 反轉鏈表
- 2. 合并有序數列
- 3. 判斷鏈表中是否有環
- 4. 鏈表排序
- 5. 判斷一個鏈表是否為回文結構
- 6. 兩個鏈表的第一個公共點
- 7. 刪除有序鏈表中重復的元素
 
簡單題
1. 反轉鏈表
輸入:[ 1 2 3]
輸出: [ 3 2 1]
public class Solution {public ListNode ReverseList(ListNode head) {// 利用指針if(head == null) return null;ListNode pre = null;ListNode next = null;while(head != null){next = head.next;head.next = pre;pre = head;head = next;}return pre;} }2. 合并有序數列
輸入 : {2},{1}
輸出:{1,2}
import java.util.*;/** public class ListNode {* int val;* ListNode next = null;* }*/public class Solution {public ListNode mergeTwoLists (ListNode l1, ListNode l2) {if(l1 == null) return l2;if(l2 == null) return l1;// 創建頭結點,指向0ListNode head = new ListNode(0);ListNode temp = head;while(l1 != null && l2 != null){if(l1.val > l2.val){temp.next = l2;l2 = l2.next;}else{temp.next = l1;l1 = l1.next;}temp = temp.next;}// 判斷是否遍歷結束temp.next = (l1 == null)? l2:l1;// 頭結點是0return head.next;} }3. 判斷鏈表中是否有環
判斷給定的鏈表中是否有環。如果有環則返回true,否則返回false。
你能給出空間復雜度[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-67B3cPuf-1623758646111)(https://www.nowcoder.com/equation?tex=O(1)]%5C)的解法么?
解題思路 - HashSet
import java.util.*; /*** Definition for singly-linked list.* class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/ public class Solution {public boolean hasCycle(ListNode head) {if(head == null) return false;Set<ListNode> set = new HashSet<>();ListNode tmp = head;while(tmp!=null){if(set.contains(tmp)){return true;}set.add(tmp);tmp = tmp.next;}return false;} }4. 鏈表排序
給定一個無序單鏈表,實現單鏈表的排序(按升序排序)。
輸入:
輸入 :[1,3,2,4,5] 返回值 :{1,2,3,4,5}解題思路 : ArrayList
import java.util.*;/** public class ListNode {* int val;* ListNode next = null;* }*/public class Solution {/*** * @param head ListNode類 the head node* @return ListNode類*/public ListNode sortInList (ListNode head) {// write code hereif(head==null) return null;ArrayList<Integer> list = new ArrayList<>();//將單鏈表中的數據放入arraylist中ListNode tmp = head;ListNode res = head;int n=0; // 記錄數組的長度while(tmp!=null){n++;list.add(tmp.val);tmp = tmp.next;}// 排序Collections.sort(list);// 取出for(int i=0; i<n; i++){res.val = list.get(i);res = res.next;}return head;} }5. 判斷一個鏈表是否為回文結構
給定一個鏈表,請判斷該鏈表是否為回文結構。
輸入 :[1 2 2 1] 返回值 :true- 回文結構 -
- 解題思路 - 棧(先進后出)
6. 兩個鏈表的第一個公共點
輸入兩個無環的單鏈表,找出它們的第一個公共結點。
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-6oC10YYj-1623758646116)(C:\Users\華為\AppData\Roaming\Typora\typora-user-images\image-20210615191432615.png)]
/* public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;} }*/ public class Solution {public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {if(pHead1==null || pHead2==null) return null;ListNode p1=pHead1;ListNode p2=pHead2;while(p1!=p2){p1=p1.next;p2=p2.next;if(p1!=p2){if(p1==null) p1 = pHead2;if(p2==null) p2 = pHead1;}}return p1;} }7. 刪除有序鏈表中重復的元素
刪除給出鏈表中的重復元素(鏈表中元素從小到大有序),使鏈表中的所有元素都只出現一次
輸入 :{1,1,2} 返回值 :{1,2} import java.util.*;/** public class ListNode {* int val;* ListNode next = null;* }*/public class Solution {/*** * @param head ListNode類 * @return ListNode類*/public ListNode deleteDuplicates (ListNode head) {// write code hereif(head==null) return head;ListNode tmp = head;while(tmp.next != null){if(tmp.val == tmp.next.val){tmp.next = tmp.next.next;}else{tmp = tmp.next;}}return head;} }總結
以上是生活随笔為你收集整理的数据结构知识点 -- 链表(Java实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 大学物理-光学
- 下一篇: BZOJ 3375: [Usaco200
