Last updated on 2023年6月7日 晚上
203.移除链表元素 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Solution { public ListNode removeElements(ListNode head, int val) { ListNode new Head = new ListNode (); ListNode resultHead = new Head ; new Head .next = head; while (head != null ){ if (head.val != val){ new Head .next = head; new Head = new Head .next; } head = head.next; } new Head .next = null ; return resultHead.next; } }
707.设计链表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 class ListNode { int val ; ListNode next; ListNode(){} ListNode(int val ){ this .val = val ; } }class MyLinkedList { int size; ListNode head; public MyLinkedList() { this .size = 0 ; this .head = new ListNode(0 ); } public int get (int index) { if (index < 0 || index >= this .size){ return -1 ; } ListNode resultNode = this .head; for (int i = 0 ; i <= index;i ++){ resultNode = resultNode.next; } return resultNode.val ; } public void addAtHead(int val ) { addAtIndex(0 , val ); } public void addAtTail(int val ) { addAtIndex(this .size, val ); } public void addAtIndex(int index, int val ) { if (index > this .size){ return ; } if (index < 0 ){ index = 0 ; } this .size++; ListNode predNode = this .head; for (int i = 0 ;i < index;i++){ predNode = predNode.next; } ListNode addNode = new ListNode(val ); addNode.next = predNode.next; predNode.next = addNode; } public void deleteAtIndex(int index) { if (index >= this .size || index < 0 ){ return ; } this .size--; if (index == 0 ){ this .head = this .head.next; return ; } ListNode predNode = head; for (int i = 0 ; i < index;i++){ predNode = predNode.next; } predNode.next = predNode.next.next; } }
206.反转链表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 /** * Definition for singly- linked list. * public class ListNode { * int val ; * ListNode next; * ListNode() {} * ListNode(int val ) { this.val = val ; } * ListNode(int val , ListNode next) { this.val = val ; this.next = next; } * } */ class Solution { public ListNode reverseList(ListNode head) { ListNode currNode = head; ListNode resultNode = null ; ListNode tmpNode = null ; while (currNode != null ){ tmpNode = currNode.next; currNode.next = resultNode; resultNode = currNode; currNode = tmpNode; } return resultNode; } }
24.两两交换链表中的结点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 class Solution { public ListNode swapPairs(ListNode head) { ListNode xuniHead = new ListNode(0 ); xuniHead.next = head; ListNode curr = xuniHead; while (curr.next != null && curr.next .next != null ){ ListNode tmp = curr.next ; ListNode tmp1 = curr.next .next .next ; curr.next = curr.next .next ; curr.next .next = tmp; curr.next .next .next = tmp1; curr = curr.next .next ; } return xuniHead.next ; } }
19. 删除链表的倒数第 N 个结点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0 ); dummy.next = head; ListNode fast = dummy; ListNode slow = dummy; for (int i = 0 ;i <= n;i++){ fast = fast.next ; } while (fast != null ){ fast = fast.next ; slow = slow.next ; } slow.next = slow.next .next ; return dummy.next ; } }
面试题02.07.链表相交 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode currA = headA; ListNode currB = headB; int lenA = 0 ; int lenB = 0 ; while (currA != null ){ currA = currA.next ; lenA++; } while (currB != null ){ currB = currB.next ; lenB++; } currA = headA; currB = headB; if (lenA < lenB){ int tmpL = lenA; lenA = lenB; lenB = tmpL; ListNode tmpNode = currA; currA = currB; currB = tmpNode; } int gap = lenA - lenB; while (gap-- > 0 ){ currA = currA.next ; } while (currA != null ){ if (currA == currB){ return currA; } currA = currA.next ; currB = currB.next ; } return null ; } }
my-leetcode-logs-20230528
https://thewangyang.github.io/2023/05/28/leetcode-notes-20230528/