目錄
2. 兩數相加
19. 刪除鏈表的倒數第 N 個結點
21. 合并兩個有序鏈表
23. 合并 K 個升序鏈表
24. 兩兩交換鏈表中的節點
2. 兩數相加
https://leetcode.cn/problems/add-two-numbers/
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {//head是cur鏈表頭節點,cur用于記錄相加和的鏈表;ListNode head = new ListNode();ListNode cur = head;//進位標志;int flag = 0;//兩個用于相加的鏈表都走到null,結束;while(l1!=null || l2!=null){int l1Val = l1==null ? 0 : l1.val;int l2Val = l2==null ? 0 : l2.val;//計算和;int sum = l1Val+l2Val+flag;//新建節點加入cur鏈表中;cur.next = new ListNode(sum%10);//判斷是否需要進位;if(sum>=10){flag=1;}else {flag=0;}//判斷兩個用于相加的鏈表是否走到null,沒到的才繼續走;if(l1!=null){l1 = l1.next;}if(l2!=null){l2 = l2.next;}//cur鏈表也向后移動;cur = cur.next;}//走到這里代表兩個鏈表已經遍歷完畢;//但是可能最后一次相加也產生了進位,因此在這里要判斷;if(flag == 1){cur.next = new ListNode(1);}//頭節點是空值,返回頭節點的下一節點;return head.next;}
19. 刪除鏈表的倒數第 N 個結點
https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
public ListNode removeNthFromEnd(ListNode head, int n) {//創建快慢指針,兩個指針的next為head;ListNode fast = new ListNode();ListNode slow = new ListNode();fast.next = head;slow.next = head;//記錄慢指針的頭節點,用于返回;ListNode root = slow;//快指針先走n步;for (int i=n;i>0;i--){fast = fast.next;//處理n大于鏈表長度的情況;if(fast == null){return null;}}//依次移動快慢指針,直到快指針的next為null;//此時代表慢指針到達要刪除的元素的前一位值;while (fast.next != null){fast = fast.next;slow = slow.next;}//刪除下一元素;slow.next = slow.next.next;//返回;return root.next;}
21. 合并兩個有序鏈表
https://leetcode.cn/problems/merge-two-sorted-lists/description/
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {//創建頭節點和一個用于移動的節點cur;ListNode head = new ListNode(0);ListNode cur = head;//邊移動,邊比較節點的值,并將比較結果賦值,直到有一個鏈表走完;while(list1 != null && list2 != null){if(list1.val <= list2.val){cur.next = list1;list1 = list1.next;}else{cur.next = list2;list2 = list2.next;}cur = cur.next;}//判斷是哪個鏈表走完了,把另一個鏈表在尾部續上;if(list1 == null){cur.next = list2;}if(list2 == null){cur.next = list1;}//返回記錄的head;return head.next;}
23. 合并 K 個升序鏈表
https://leetcode.cn/problems/merge-k-sorted-lists/description/
public ListNode mergeKLists(ListNode[] lists) {//建立鏈表;ListNode root = null;//不斷將鏈表元素兩兩合并;for (ListNode list : lists) {root = mergeTwoLists(root, list);}//返回鏈表;return root;}public ListNode mergeTwoLists(ListNode list1, ListNode list2) {//創建頭節點和一個用于移動的節點cur;ListNode head = new ListNode(0);ListNode cur = head;//邊移動,邊比較節點的值,并將比較結果賦值,直到有一個鏈表走完;while(list1 != null && list2 != null){if(list1.val <= list2.val){cur.next = list1;list1 = list1.next;}else{cur.next = list2;list2 = list2.next;}cur = cur.next;}//判斷是哪個鏈表走完了,把另一個鏈表在尾部續上;if(list1 == null){cur.next = list2;}if(list2 == null){cur.next = list1;}//返回記錄的head;return head.next;}
24. 兩兩交換鏈表中的節點
https://leetcode.cn/problems/swap-nodes-in-pairs/
public ListNode swapPairs(ListNode head) {//基本思路就是站在前一個節點(后續簡稱0節點),向后望兩個節點。//并對這兩個節點做順序調換,之后0節點向后移動;//創建移動的節點指針,這個節點指向head,這個就是一開始的0節點;ListNode cur = new ListNode();cur.next = head;//記錄頭節點地址,用于返回;ListNode root = cur;while(true){//沒有后續節點的情況;if(cur.next == null){break;}//仍存在兩個后續節點的情況;if(cur.next.next != null){//記錄下一次需要調整順序的節點;ListNode temp2 = cur.next.next.next;//調整順序;ListNode temp1 = cur.next;cur.next = cur.next.next;cur = cur.next;cur.next = temp1;//將節點移動到下一次調整的0節點處;cur = cur.next;cur.next = temp2;}else{//只剩一個節點的情況;break;}}//代碼運行到這代表,0節點之后已經沒有節點或只剩一個節點;//返回記錄的頭節點;return root.next;}