链表定义
1 2 3 4 5 6 7 8 9 10 11 12 13 public class ListNode (){ int val; ListNode next; public ListNode () {} public ListNode (int val) { this .val = val; } public ListNode (int val,ListNode next) { this .val =val this .next = next; } }
1 2 3 4 5 public class MyListNode (){ int size; ListNode head; }
设计链表
你可以选择使用单链表或者双链表,设计并实现自己的链表。
单链表中的节点应该具备两个属性:val 和 next 。val 是当前节点的值,next 是指向下一个节点的指针/引用。
如果是双向链表,则还需要属性 prev 以指示链表中的上一个节点。假设链表中的所有节点下标从 0 开始。
实现 MyLinkedList 类:
MyLinkedList() 初始化 MyLinkedList 对象。
int get(int index) 获取链表中下标为 index 的节点的值。如果下标无效,则返回 -1 。
void addAtHead(int val) 将一个值为 val 的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。
void addAtTail(int val) 将一个值为 val 的节点追加到链表中作为链表的最后一个元素。
void addAtIndex(int index, int val) 将一个值为 val 的节点插入到链表中下标为 index 的节点之前。如果 index 等于链表的长度,那么该节点会被追加到链表的末尾。如果 index 比长度更大,该节点将 不会插入 到链表中。
void deleteAtIndex(int index) 如果下标有效,则删除链表中下标为 index 的节点。
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 class MyLinkedList { int size; ListNode head; public MyLinkedList () { size = 0 ; ListNode head = new ListNode (0 ); } public int get (int index) { if (index < 0 || index >= size){ return -1 ; } ListNode current = head; for (int i= 0 ;i<index;i++){ current = current.next; } return current.val; } public void addAtHead (int val) { addAtIndex(0 ,val); } public void addAtTail (int val) { addAtIndex(size,val); } public void addAtIndex (int index, int val) { if (index > size) { return ; } index = Math.max(0 , index); size++; ListNode pred = head; ListNode toAdd = new ListNode (val); for (int i = 0 ; i < index; i++) { pred = pred.next; } toAdd.next = pred.next; pred.next = toAdd; } public void deleteAtIndex (int index) { if (index <0 || index >= size){ return ; } size --; ListNode pred = head; for (int i=0 ;i<index;i++){ pred= pred.next; } pred.next =pred.next.next; } } class ListNode { int val; ListNode next; public ListNode (int val) { this .val = val; } }
反转链表
206. 反转链表 - 力扣(LeetCode)
就是将链表的指针倒过来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution { public ListNode reverseList (ListNode head) { ListNode rel = null ; ListNode cur = head; ListNode temp; while (cur !=null ){ temp = cur.next; cur.next = rel; rel = cur; cur = temp; } return rel; } }
两两交换链表中的节点
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)
24. 两两交换链表中的节点 - 力扣(LeetCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution { public ListNode swapPairs (ListNode head) { ListNode dummyHead = new ListNode (0 ); dummyHead.next = head; ListNode temp = dummyHead; while (temp.next != null && temp.next.next != null ){ ListNode node1 = temp.next ; ListNode node2 = temp.next.next; temp.next = node2; node1.next = node2.next; node2.next = node1; temp = node1; } return dummyHead.next; } }
删除链表的倒数第N个结点
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
1 2 输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode cur = head; if (cur.next == null){ return null; } int count = 0 ; while(cur != null){ count ++; cur = cur.next; } ListNode dummyHead = new ListNode(); dummyHead.next =head; cur = dummyHead; int index = count - n +1 ; for (int i=0 ;i<index-1 ;i++){ cur = cur.next; } cur.next = cur.next.next; return dummyHead.next; } }
链表相交
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
1 2 3 4 5 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 输出:Intersected at '8' 解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。 从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。 在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class Solution { public ListNode getIntersectionNode (ListNode headA, ListNode headB) { Set<ListNode> visited = new HashSet <ListNode>(); ListNode temp = headA; while (temp != null ){ visited.add(temp); temp = temp.next; } temp = headB; while (temp!=null ){ if (visited.contains(temp)){ return temp; } temp = temp.next; } return null ; } }