LeetCode::LinkedList(Part2)-Modifying the List
Introduction
Most linked list algorithms are logially quite straightforward. The main difficult lies in that fact that when working on a node in the list, often times you also need to have reference to the node just before it or after it. Take for example a simple case of deleting the node B
from list A->B->C
. After you identify where B
is, you still need to keep a reference of A
in order to write A->next = B->next
.
This brings code complexity, as you need to keep track of more variables and treat special cases carefully. The head node obviously has no previous node and the NULL
node has no node after it. I usually approach a problem in the following steps:
-
Design the inside of
while
loopIn this step we ignore edge cases and focus on how to modify the body of the list. A
cur
node, initialized as thehead
node before we enter the loop, will always be the current node that we are working on. -
Decide the break point for the loop
This will usually be when our
cur
node reachesNULL
, but exceptions exsist. For example, if we referencedcur->next->value
in the loop, since aNULL
node has no value filed, we must break out of the loop whencur->next
isNULL
. -
Handle edge cases
The
head
node usually needs to be handled seperately.
83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example:
Input: 1->1->2->3->3
Output: 1->2->3
Solution 1 (Iterative)
Solution 2 (Recursive)
328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node index and not the value in the nodes.
Example:
Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL
Note:
- 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
- You should try to do it in place.
Solution
- loop
first-> next = first->next->next can we do it in two passes? No so add second whats the break point whats the base case
When you need the node Before cur
82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Solution 1 (Iterative)
203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val
.
Example:
Input: 1->2->6->3->4->5->6
, val = 6
Output: 1->2->3->4->5
Solution 1
When you need nodes Before and After cur
put struct ListNode* after
in the loop, so your while loop doesnt break.
206. Reverse Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Solution 1 (Iterative)
When you need the node after temp
As you can see, although we have to change the loop condition accordingly, the logic works exactly the same.
The Python version of the same code is quite a bit shorter:
This is the most popular solution on LeetCode, is 1 line shorter compared to using cur
and nex
, but perfonally I don’t like it much. Mainly because such a use of pre
is the most typical and may confuse people.
Solution 2 (Recursive)
After the first recursive call, we have:
while tail
is at 5 and head
still at 1. We fix the 1->2
link with (head->next)->next = head
and add 1->NULL
.
24. Swap Nodes in Pairs
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:
Input: 1->2->3->4
Output: 2->1->4->3