203. 移除链表元素 easy 简单题,看看CPP的指针的写法就好
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 class Solution {public : ListNode* removeElements (ListNode* head, int val) { ListNode * h=new ListNode (); h->next = head; ListNode *t = h; while (t->next != nullptr ){ if (t->next->val == val){ ListNode * temp = t->next; t->next = t->next->next; delete temp; }else t = t->next; } return h->next; } }; class Solution {public : ListNode* removeElements (ListNode* head, int val) { if (head == nullptr ) { return head; } head->next = removeElements (head->next, val); return head->val == val ? head->next : head; } };
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 class Solution { public ListNode removeElements (ListNode head, int val) { if (head == null ){ return head; } ListNode hh = new ListNode (); ListNode h = hh; h.next = head; while (h.next != null ){ if (h.next.val == val){ h.next = h.next.next; }else { h = h.next; } } return hh.next; } } class Solution { public ListNode removeElements (ListNode head, int val) { if (head == null ){ return head; } head.next = removeElements(head.next, val); return head.val == val? head.next:head; } }
707.设计链表 middle 考察基础,并不难,主要理解在cpp在class中创建struct或者java在class中创建class。
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 class MyLinkedList {public : struct LinkedNode { int val; LinkedNode * next; LinkedNode () : val (0 ), next (nullptr ) {} LinkedNode (int x) : val (x), next (nullptr ) {} LinkedNode (int x, LinkedNode *next) : val (x), next (next) {} }; MyLinkedList () { preHead = new LinkedNode (); _size = 0 ; } int get (int index) { if (index > (_size - 1 ) || index < 0 ) { return -1 ; } LinkedNode * t = preHead->next; while (index--){ t = t->next; } return t->val; } void addAtHead (int val) { LinkedNode * ins = new LinkedNode (val,preHead->next); preHead->next = ins; _size++; } void addAtTail (int val) { LinkedNode * ins = new LinkedNode (val,nullptr ); LinkedNode* cur = preHead; while (cur->next != nullptr ){ cur = cur->next; } cur->next = ins; _size++; } void addAtIndex (int index, int val) { if (index > _size) return ; if (index < 0 ) index = 0 ; LinkedNode * ins = new LinkedNode (val); LinkedNode * t = preHead; while (index--){ t = t->next; } ins->next = t->next; t->next = ins; _size++; return ; } void deleteAtIndex (int index) { if (index < 0 || index >= _size) return ; LinkedNode * t = preHead; while (index--){ t = t->next; } LinkedNode* tmp = t->next; t->next = t->next->next; _size--; delete tmp; return ; } private : int _size; LinkedNode* preHead; };
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 class MyLinkedList { class ListNode { int val; ListNode next; ListNode(int val) { this .val=val; } } private int size; private ListNode head; public MyLinkedList () { this .size = 0 ; this .head = new ListNode (0 ); } public int get (int index) { if (index < 0 || index >= size) { return -1 ; } ListNode cur = head; for (int i = 0 ; i <= index; i++) { cur = cur.next; } return cur.val; } public void addAtHead (int val) { ListNode newNode = new ListNode (val); newNode.next = head.next; head.next = newNode; size++; } public void addAtTail (int val) { ListNode newNode = new ListNode (val); ListNode cur = head; while (cur.next != null ) { cur = cur.next; } cur.next = newNode; size++; } public void addAtIndex (int index, int val) { if (index < 0 || index > size) { return ; } ListNode pre = head; for (int i = 0 ; i < index; i++) { pre = pre.next; } ListNode newNode = new ListNode (val); newNode.next = pre.next; pre.next = newNode; size++; } public void deleteAtIndex (int index) { if (index < 0 || index >= size) { return ; } ListNode pre = head; for (int i = 0 ; i < index ; i++) { pre = pre.next; } pre.next = pre.next.next; size--; } }