Leetcode-Question-147: Insertion Sort List

原文链接:
https://leetcode.com/problems/insertion-sort-list/

题目描述:
147. Insertion Sort List
Difficulty: Medium

Sort a linked list using insertion sort.

解析:

 

Leetcode-Question-86: Partition List

原文链接:
https://leetcode.com/problems/partition-list/

题目描述:
86. Partition List
Difficulty: Medium

Given 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.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

解析:拆解链表,小的一边,大的一边,然后再合。具体解法给出两个:

法一)朴素的拆解法:

法二)额外申请头结点

 

Leetcode-Question-234: Palindrome Linked List

原文链接:
https://leetcode.com/problems/palindrome-linked-list/

题目描述:
234. Palindrome Linked List
Difficulty: Easy

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

解析:

 

Leetcode-Question-203: Remove Linked List Elements

原文链接:

https://leetcode.com/problems/remove-linked-list-elements/

题目描述:

Remove Linked List Elements
Difficulty: Easy

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
Return: 1 –> 2 –> 3 –> 4 –> 5

解答:

 

或者,添加一个头节点: