2016年12月2日 星期五

[LeetCode] 25. Reverse Nodes in k-Group

轉自LeetCode

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For 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

<Solution>

[LeetCode] 24. Swap Nodes in Pairs

轉自LeetCode

Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
<Solution>

2016年12月1日 星期四

[LeetCode] 23. Merge k Sorted Lists

轉自LeetCode

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

<Solution>

[LeetCode] 22. Generate Parentheses

轉自LeetCode

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]
<Solution>

[LeetCode] 21. Merge Two Sorted Lists

轉自LeetCode

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

<Solution>

[LeetCode] 20. Valid Parentheses

轉自LeetCode

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
<Solution>

[LeetCode] 19. Remove Nth Node From End of List

轉自LeetCode

Given a linked list, remove the nth node from the end of list and return its head.
For 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.
Try to do this in one pass.
<Solution>