Leetcode - Linked lists
Linked Lists
2. Add Two Numbers
https://leetcode.com/problems/add-two-numbers/
Description
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
1 | Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) |
Solution
https://leetcode.com/problems/add-two-numbers/solution/
https://leetcode.com/problems/add-two-numbers/discuss/1032/Python-concise-solution.
1 | # Definition for singly-linked list. |
19. Remove Nth Node From End of List[M]
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
Description
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
1 | Given linked list: 1->2->3->4->5, and n = 2. |
Note:
Given n will always be valid.
Follow up:
Could you do this in one pass?
Solution
https://leetcode.com/problems/remove-nth-node-from-end-of-list/solution/
1 | # Definition for singly-linked list. |
21. Merge Two Sorted Lists[E]
Description
Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.
Example:
1 | Input: 1->2->4, 1->3->4 |
Solution
1 | # Definition for singly-linked list. |
23. Merge k Sorted Lists[H]
https://leetcode.com/problems/merge-k-sorted-lists/
Description
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
1 | Input: |
Solution
1 |
24. Swap Nodes in Pairs
https://leetcode.com/problems/swap-nodes-in-pairs/
Description
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list’s nodes, only nodes itself may be changed.
Example:
1 | Given 1->2->3->4, you should return the list as 2->1->4->3. |
Solution
1 | # Definition for singly-linked list. |
1 |
25. Reverse Nodes in k-Group[H]
https://leetcode.com/problems/reverse-nodes-in-k-group/
Description
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
Example:
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
Note:
- Only constant extra memory is allowed.
- You may not alter the values in the list’s nodes, only nodes itself may be changed.
Solution
1 | # Definition for singly-linked list. |
61. Rotate List[M]
https://leetcode.com/problems/rotate-list/
Description
Given a linked list, rotate the list to the right by k places, where k is non-negative.
Example 1:
1 | Input: 1->2->3->4->5->NULL, k = 2 |
Example 2:
1 | Input: 0->1->2->NULL, k = 4 |
Solution
1 |
92. Reverse Linked List II[M]
https://leetcode.com/problems/reverse-linked-list-ii/
Description
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
1 | Input: 1->2->3->4->5->NULL, m = 2, n = 4 |
Solution
1 |
141. Linked List Cycle[E]
https://leetcode.com/problems/linked-list-cycle/
Description
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Example 1:
1 | Input: head = [3,2,0,-4], pos = 1 |

Example 2:
1 | Input: head = [1,2], pos = 0 |

Example 3:
1 | Input: head = [1], pos = -1 |

Follow up:
Can you solve it using O(1) (i.e. constant) memory?
Solution
https://leetcode.com/problems/linked-list-cycle/solution/
1 | # Definition for singly-linked list. |
142. Linked List Cycle II[M]
https://leetcode.com/problems/linked-list-cycle-ii/
Description
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Note: Do not modify the linked list.
Example 1:
1 | Input: head = [3,2,0,-4], pos = 1 |

Example 2:
1 | Input: head = [1,2], pos = 0 |

Example 3:
1 | Input: head = [1], pos = -1 |

Follow-up:
Can you solve it without using extra space?
Solution
https://discuss.leetcode.com/topic/2975/o-n-solution-by-using-two-pointers-without-change-anything
1 | # Definition for singly-linked list. |
160. Intersection of Two Linked Lists[E]
https://leetcode.com/problems/intersection-of-two-linked-lists/
Description
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
begin to intersect at node c1.
Example 1:
1 | Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 |
Example 2:
1 | Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 |
Example 3:
1 | Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 |
Notes:
- If the two linked lists have no intersection at all, return
null. - The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Each value on each linked list is in the range
[1, 10^9]. - Your code should preferably run in O(n) time and use only O(1) memory.
Solution
https://leetcode.com/articles/intersection-two-linked-lists/
https://leetcode.com/problems/intersection-of-two-linked-lists/solution/
1 | # Definition for singly-linked list. |
203. Remove Linked List Elements[E]
https://leetcode.com/problems/remove-linked-list-elements/
Description
Remove all elements from a linked list of integers that have value *val*.
Example:
1 | Input: 1->2->6->3->4->5->6, val = 6 |
Solution
1 | # Definition for singly-linked list. |
206. Reverse Linked List[E]
https://leetcode.com/problems/reverse-linked-list/
Description
Reverse a singly linked list.
Example:
1 | Input: 1->2->3->4->5->NULL |
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Solution
1 |
237. Delete Node in a Linked List[E]
https://leetcode.com/problems/delete-node-in-a-linked-list/
Description
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Given linked list – head = [4,5,1,9], which looks like following:

Example 1:
1 | Input: head = [4,5,1,9], node = 5 |
Example 2:
1 | Input: head = [4,5,1,9], node = 1 |
Note:
- The linked list will have at least two elements.
- All of the nodes’ values will be unique.
- The given node will not be the tail and it will always be a valid node of the linked list.
- Do not return anything from your function.
Solution
1 | # Definition for singly-linked list. |
328. Odd Even Linked List[M]
https://leetcode.com/problems/odd-even-linked-list/
Description
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example 1:
1 | Input: 1->2->3->4->5->NULL |
Example 2:
1 | Input: 2->1->3->5->6->4->7->NULL |
Constraints:
- The relative order inside both the even and odd groups should remain as it was in the input.
- The first node is considered odd, the second node even and so on …
- The length of the linked list is between
[0, 10^4].
Solution
1 |
876. Middle of the Linked List[E]
https://leetcode.com/problems/middle-of-the-linked-list/
Description
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:
1 | Input: [1,2,3,4,5] |
Example 2:
1 | Input: [1,2,3,4,5,6] |
Note:
- The number of nodes in the given list will be between
1and100.
Solution
1 | # Definition for singly-linked list. |
143. Reorder List[M]]
https://leetcode.com/problems/reorder-list/
Description
Given a singly linked list L: L0→L1→…→L**n-1→Ln,
reorder it to: L0→L**n→L1→L**n-1→L2→L**n-2→…
You may not modify the values in the list’s nodes, only nodes itself may be changed.
Example 1:
1 | Given 1->2->3->4, reorder it to 1->4->2->3. |
Example 2:
1 | Given 1->2->3->4->5, reorder it to 1->5->2->4->3. |
Solution
1 |
138. Copy List with Random Pointer[M]
https://leetcode.com/problems/copy-list-with-random-pointer/
Description
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
The Linked List is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
val: an integer representingNode.valrandom_index: the index of the node (range from0ton-1) where random pointer points to, ornullif it does not point to any node.
Example 1:

1 | Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]] |
Example 2:

1 | Input: head = [[1,1],[2,1]] |
Example 3:

1 | Input: head = [[3,null],[3,0],[3,null]] |
Example 4:
1 | Input: head = [] |
Constraints:
-10000 <= Node.val <= 10000Node.randomis null or pointing to a node in the linked list.- Number of Nodes will not exceed 1000.
Solution
1 |
147. Insertion Sort List[M]
https://leetcode.com/problems/insertion-sort-list/
Description
ort a linked list using insertion sort.

A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list
Algorithm of Insertion Sort:
- Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
- At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
- It repeats until no input elements remain.
Example 1:
1 | Input: 4->2->1->3 |
Example 2:
1 | Input: -1->5->3->4->0 |
Solution
1 |
83. Remove Duplicates from Sorted List[E]
https://leetcode.com/problems/remove-duplicates-from-sorted-list/
Description
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
1 | Input: 1->1->2 |
Example 2:
1 | Input: 1->1->2->3->3 |
Solution
1 | # Definition for singly-linked list. |
86. Partition List[M]
https://leetcode.com/problems/partition-list/
Description
iven a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Example:
1 | Input: head = 1->4->3->2->5->2, x = 3 |
Solution
https://leetcode.com/problems/partition-list/solution/
1 |









