Posted onEdited onDisqus: Word count in article: 9.5kReading time ≈9 mins.
Design Linked List
This is the solution for the “Design Linked List” problem on LeetCode. It creates a class called MyLinkedList which implements the functionality of a linked list.
funcaddAtIndex(_index: Int, _val: Int) { if index >=0&& index <= count, let node = findNode(index) { let newNode =Node(val) newNode.next = node newNode.prev = node.prev node.prev?.next = newNode node.prev = newNode count +=1 } }
funcdeleteAtIndex(_index: Int) { if index >=0&& index < count, let node = findNode(index) { node.prev?.next = node.next node.next?.prev = node.prev node.prev =nil node.next =nil count -=1 } }
privatefuncfindNode(_index: Int) -> Node? { var p: Node? var i =0 if index <= (count/2) { p = head.next while p !=nil, i < index { p = p?.next i +=1 } } else { p = tail i = count while p !=nil, i > index { p = p?.prev i -=1 } } return p }
privatefuncprintList() { var p = head.next var i: Int=0 while p !=nil, i < count { print(p!.val, terminator: "->" ) p = p?.next i +=1 } print("") } }
Linked List Cycle
This is the solution for the “Linked List Cycle” problem on LeetCode. It contains a class called Solution which checks whether a linked list contains a cycle.
1 2 3 4 5 6 7 8 9 10 11
classSolution{ funchasCycle(_head: ListNode?) -> Bool { var fast = head?.next var slow = head while fast !=nil, fast !== slow { fast = fast?.next?.next slow = slow?.next } return fast !=nil } }
Linked List Cycle II
This is the solution for the “Linked List Cycle II” problem on LeetCode. It contains a class called Solution which finds the node where the cycle begins in a linked list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution{ funcdetectCycle(_head: ListNode?) -> ListNode? { var slow = head var fast = head?.next while fast !=nil, fast !== slow { fast = fast?.next?.next slow = slow?.next } slow = head fast = fast?.next while fast !=nil, fast !== slow { fast = fast?.next slow = slow?.next } return fast } }
Intersection of Two Linked Lists
This is the solution for the “Intersection of Two Linked Lists” problem on LeetCode. It contains a class called Solution which finds the intersection node of two linked lists.
classSolution{ funcgetIntersectionNode(_headA: ListNode?, _headB: ListNode?) -> ListNode? { var tailA = headA whilelet p = tailA?.next { tailA = p } tailA?.next = headB
let fast = detectCycle(headA)
tailA?.next =nil
return fast }
privatefuncdetectCycle(_head: ListNode?) -> ListNode? { var slow = head var fast = head?.next while fast !=nil, fast !== slow { fast = fast?.next?.next slow = slow?.next } slow = head fast = fast?.next while fast !=nil, fast !== slow { fast = fast?.next slow = slow?.next } return fast } }
Remove Nth Node From End of List
This is the solution for the “Remove Nth Node From End of List” problem on LeetCode. It contains a class called Solution which removes the nth node from the end of a linked list.
classSolution{ funcreverseList(_head: ListNode?) -> ListNode? { if head?.next ==nil { return head } let h = reverseList(head?.next) head?.next?.next = head head?.next =nil return h }
funcreverseList(_head: ListNode?) -> ListNode? { var prev: ListNode? =nil var cur = head while cur !=nil { let p = cur?.next cur?.next = prev prev = cur cur = p } return prev } }
Remove Linked List Elements
This is the solution for the “Remove Linked List Elements” problem on LeetCode. It contains a class called Solution which removes all elements of a linked list that have a certain value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution{ funcremoveElements(_head: ListNode?, _val: Int) -> ListNode? { let pHead =ListNode(-1, head) var p: ListNode? = pHead while p !=nil { iflet pn = p!.next, pn.val == val { p!.next = pn.next } else { p = p!.next } } return pHead.next } }
Odd Even Linked List
This is the solution for the “Odd Even Linked List” problem on LeetCode. It contains a class called Solution which rearranges a linked list such that all the odd nodes come before the even nodes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution{ funcoddEvenList(_head: ListNode?) -> ListNode? { let evenHead = head let oddHead = head?.next var evenP = evenHead var oddP = oddHead while oddP?.next !=nil { evenP?.next = evenP?.next?.next oddP?.next = oddP?.next?.next evenP = evenP?.next oddP = oddP?.next } evenP?.next = oddHead return evenHead } }
Palindrome Linked List
This is the solution for the “Palindrome Linked List” problem on LeetCode. It contains a class called Solution which checks whether a linked list is a palindrome.
privatefunccompare(_head: ListNode?, _tail: ListNode?) -> Bool { var p = head var q = tail while q !=nil { if p ==nil|| q!.val != p!.val { returnfalse } p = p!.next q = q!.next } returntrue }
privatefunccenter(_head: ListNode?) -> ListNode? { var fast = head?.next var slow = head while fast !=nil, fast !== slow { fast = fast?.next?.next slow = slow?.next } return slow }
privatefuncreverseList(_head: ListNode?) -> ListNode? { var prev: ListNode? =nil var cur = head while cur !=nil { let p = cur?.next cur?.next = prev prev = cur cur = p } return prev } }
Merge Two Sorted Lists
This is the solution for the “Merge Two Sorted Lists” problem on LeetCode. It contains a class called Solution which merges two sorted linked lists into a single sorted linked list.
This is the solution for the “Add Two Numbers” problem on LeetCode. It contains a class called Solution which adds two numbers represented as linked lists.
classSolution{ funcaddTwoNumbers(_l1: ListNode?, _l2: ListNode?) -> ListNode? { let head =ListNode(-1) var p1 = l1 var p2 = l2 var p3: ListNode? = head var v =0 var carry =0 while p1 !=nil|| p2 !=nil { v = (p1?.val ??0) + (p2?.val ??0) + carry carry = v/10 v %=10 p3?.next =ListNode(v) p1 = p1?.next p2 = p2?.next p3 = p3?.next } if carry !=0 { p3?.next =ListNode(carry) } return head.next } }
Flatten a Multilevel Doubly Linked List
This is the solution for the “Flatten a Multilevel Doubly Linked List” problem on LeetCode. It contains a class called Solution which flattens a multilevel doubly linked list.
classSolution{ funcflatten(_head: Node?) -> Node? { getTail(head) return head }
privatefuncgetTail(_head: Node?) -> Node? { guardlet head = head else { returnnil } iflet child = head.child { let tail = getTail(child) tail?.next = head.next head.next?.prev = tail head.next = head.child head.child?.prev = head head.child =nil } iflet p = head.next { return getTail(p) } return head } }
Insert into a Cyclic Sorted List
This is the solution for the “Insert into a Cyclic Sorted List” problem on LeetCode. It contains a class called Solution which inserts a node into a cyclic sorted list.
classSolution{ funcinsert(_head: Node?, _insertVal: Int) -> Node? { if head ==nil { let n =Node(insertVal) n.next = n return n }
var p = head while p !=nil { if (p!.val <= insertVal && p!.next!.val >= insertVal) || (p!.val > p!.next!.val && (insertVal <= p!.next!.val || insertVal >= p!.val ) || p!.next == head ) { let n =Node(insertVal) n.next = p!.next p!.next = n break } p = p!.next }
return head } }
Copy List with Random Pointer
This is the solution for the “Copy List with Random Pointer” problem on LeetCode. It contains a class called Solution which creates a deep copy of a linked list with random pointers.
classSolution{ funccopyRandomList(_head: Node?) -> Node? { var p = head var nNode = head while p !=nil { let n =Node(p!.val) n.next = p?.next p?.next = n p = p?.next?.next } p = head while p !=nil { p?.next?.random = p?.random?.next p = p?.next?.next } p = head nNode = head?.next while p !=nil { let q = p?.next p?.next = p?.next?.next q?.next = q?.next?.next p = p?.next } return nNode } }
Rotate List
This is the solution for the “Rotate List” problem on LeetCode. It contains a class called Solution which rotates a linked list to the right by k places.