1 . Reverse Linked List
/**
1. Definition for singly-linked list.
2. function ListNode(val) {
3. this.val = val;
4. this.next = null;
5. }
*/
/**
6. @param {ListNode} head
7. @return {ListNode}
*/
var reverseList = function(head) {
var currNode = head;
var prevNode = null;
var nextNode = null;
while(currNode != null){
nextNode = currNode.next;
currNode.next = prevNode;
prevNode = currNode;
currNode = nextNode;
}
return prevNode;
};
2 . Merge Two Sorted Lists
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.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function(l1, l2) {
var dummy = new ListNode(0);
var curr = dummy;
while(l1 != null && l2 != null){
if(l1.val >= l2.val){
curr.next = l2;
l2 = l2.next;
}else{
curr.next = l1;
l1 = l1.next;
}
curr = curr.next;
}
if(l1 != null){
curr.next = l1;
}
if(l2 != null){
curr.next = l2;
}
return dummy.next;
};
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。