设计链表
https://leetcode-cn.com/problems/design-linked-list/
class MyLinkedList {private Node first;private Node last;private int n = 0;class Node{Node next = null;int val;}public MyLinkedList() {}/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */public int get(int index) {if(index > n - 1) //超出范围返回-1return -1;else{Node r = first;while(index > 0){r = r.next;index--;}return r.val;}}/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */public void addAtHead(int val) {Node temp = new Node();temp.val = val;if(first == null){ //若链表为空first = last = temp;}else{temp.next = first;first = temp;}n++;}/** Append a node of value val to the last element of the linked list. */public void addAtTail(int val) {Node temp = new Node();temp.val = val;if(last == null){ //若链表为空first = last = temp;}else{last.next = temp;last = temp;}n++;}/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */public void addAtIndex(int index, int val) {if(index <= n){ //插入位置合法if(index == 0){addAtHead(val);return;}Node pre = first;while(index > 1){pre = pre.next;index--;}Node temp = new Node();temp.val = val;temp.next = pre.next;pre.next = temp;if(pre == last) //若插入到最后一个节点,移动lastlast = temp;n++;}}/** Delete the index-th node in the linked list, if the index is valid. */public void deleteAtIndex(int index) {if(index + 1 <= n){if(index == 0){first = first.next;return;}Node pre = first;while(index > 1){pre = pre.next;index--;}Node del = pre.next;pre.next = del.next;if(del == last) //若删除的最后一个节点,移动lastlast = pre;n--;}}}
双指针
环形链表
https://leetcode-cn.com/problems/linked-list-cycle/
bool hasCycle(struct ListNode *head) {struct ListNode * fast;struct ListNode * slow;fast = slow = head;while(fast){if(fast->next){fast = fast->next->next;slow = slow->next;if(fast == slow)return true;}else return false;}return false;}
public class Solution {public boolean hasCycle(ListNode head) {ListNode slow = head;ListNode fast = head;int i;if(head == null)return false;//注意一开始快慢指针是相等的,需要排除for(i = 0; !(fast == slow && i != 0); i++){slow = slow.next;fast = fast.next;if(fast != null && fast.next != null)fast = fast.next;else return false;}return true;}}
环形链表 II
https://leetcode-cn.com/problems/linked-list-cycle-ii/
struct ListNode *detectCycle(struct ListNode *head) {struct ListNode * fast;struct ListNode * slow;fast = slow = head;while(fast){if(fast->next){fast = fast->next->next;slow = slow->next;if(fast == slow){struct ListNode * p = head;while(p != slow){slow = slow->next;p = p->next;}return p;}}else return false;}return false;}
public class Solution {public ListNode detectCycle(ListNode head) {ListNode slow = head;ListNode fast = head;int i;if(head == null)return null;//注意一开始快慢指针是相等的,需要排除for(i = 0; !(fast == slow && i != 0); i++){slow = slow.next;fast = fast.next;if(fast != null && fast.next != null)fast = fast.next;else return null;}ListNode temp = head;//当slow与fast相遇时,再使用一个指针ptr指向链表头部,它和 slow 每次向后移动一个位置,最终会在入环点相遇,具体公式推导见原网页while(temp != slow){slow = slow.next;temp = temp.next;}return temp;}}
相交链表
https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {struct ListNode *a = headA;struct ListNode *b = headB;bool flagA = false;bool flagB = false;while(a && b){if(a == b){return a;}a = a->next;b = b->next;if(!a && !flagA){a = headB;flagA = true;}if(!b && !flagB){b = headA;flagB = true;}}return false;}
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode a = headA;ListNode b = headB;if(headA == null || headB == null)return null;while(a != b){a = a.next;b = b.next;if(a == b && b == null)return null;if(a == null)a = headB;if(b == null)b = headA;}return a;}}
删除链表的倒数第 N 个结点
https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){struct ListNode* p = head;struct ListNode* q = head;n++;while(n != 0){if(p){p = p->next;n--;}else{//删除头结点head = head->next;return head;}}while(p){p = p->next;q = q->next;}q->next = q->next->next;return head;}
class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode fast = head;ListNode slow = head;ListNode pre = null;while(n > 1){fast = fast.next;n--;}while(fast.next != null){fast = fast.next;pre = slow;slow = slow.next;}//删除头节点if(pre == null)return head.next;//删除尾节点if(slow == fast)pre.next = null;else pre.next = slow.next;return head;}}
经典问题
反转链表
https://leetcode-cn.com/problems/reverse-linked-list/
struct ListNode* reverseList(struct ListNode* head){struct ListNode* pre = NULL;struct ListNode* cur = head;while(cur){struct ListNode* next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}
//递归//反转以head为头结点的链表,返回反转后的头结点struct ListNode* reverseList(struct ListNode* head){if(head == NULL || head->next == NULL)return head;struct ListNode* rev = reverseList(head->next);head->next->next = head;head->next = NULL;return rev;}
class Solution {public ListNode reverseList(ListNode head) {ListNode pre = null;ListNode cur = head;while(cur != null){ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;}return pre;}}
//递归class Solution {public ListNode reverseList(ListNode head) {if(head == null || head.next == null){return head;}ListNode rev = reverseList(head.next);head.next.next = head;head.next = null;return rev;}}
移除链表元素
https://leetcode-cn.com/problems/remove-linked-list-elements/
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* pre = NULL;
struct ListNode* cur = head;
while(cur){
if(cur->val == val){
//删除头结点
if(pre == NULL){
cur = cur->next;
head = head->next;
continue;
}
pre->next = cur->next;
}
else {
//没有删除, pre 向前移动
pre = cur;
}
cur = cur->next;
}
return head;
}
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode start = new ListNode(-1);
start.next = head;
ListNode pre = start;
ListNode cur = head;
while(cur != null){
if(cur.val == val)
pre.next = cur.next;
else
pre = pre.next;
cur = cur.next;
}
//注意不能直接返回head
return start.next;
}
}
