Press "Enter" to skip to content

Posts tagged as “list”

花花酱 LeetCode 19. Remove Nth Node From End of List

Problem

Given a linked list, remove the n-th node from the end of list and return its head.

Example:

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:

Given n will always be valid.

Follow up:

Could you do this in one pass?

Solution 0: Cheating! store the nodes in an array

C++

Solution 1: Two passes

Time complexity: O(L)

Space complexity: O(1)

C++

Solution 2: Fast/Slow Pointers + Dummy Head / Prev

Fast pointer moves n steps first, and then slow pointer starts moving.

When fast pointer reaches tail, slow pointer is n-th node from the end.

Time complexity: O(L)

Space complexity: O(1)

C++

Java

Python3

花花酱 LeetCode 141. Linked List Cycle

Problem

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

Solution1: HashTable

Time complexity: O(n)

Space complexity: O(n)

Solution2: Fast + Slow pointers

Time complexity: O(n)

Space complexity: O(1)

 

花花酱 LeetCode 707. Design Linked List

Problem

Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and nextval is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

  • get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  • addAtHead(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.
  • addAtTail(val) : Append a node of value val to the last element of the linked list.
  • addAtIndex(index, val) : 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.
  • deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:

Note:

  • All values will be in the range of [1, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in LinkedList library.




Solution: Single linked list

Keep tracking head and tail of the list.

Time Complexity:

addAtHead, addAtTail O(1)

addAtIndex O(index)

deleteAtIndex O(index)

Space complexity: O(1)

C++

Tracking head/tail and size of the list.

v2

Python3

Java

 

花花酱 LeetCode 876. Middle of the Linked List

Problem

Given a non-empty, singly linked list with head node head, return a middle node of linked list.

If there are two middle nodes, return the second middle node.

Example 1:

Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3.  (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.

Example 2:

Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.

 

Note:

  • The number of nodes in the given list will be between 1 and 100.

Solution: Slow + Fast Pointers

Time complexity: O(n)

Space complexity: O(1)

 

花花酱 LeetCode 148. Sort List

Problem

Sort a linked list in O(n log n) time using constant space complexity.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

Solution: Merge Sort

Top-down (recursion)

Time complexity: O(nlogn)

Space complexity: O(logn)

C++

Java

Python3

 

bottom up

Time complexity: O(nlogn)

Space complexity: O(1)