Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
ListNode previous = null;
while (head != null) {
ListNode nextNode = head.next;
head.next=previous;
previous = head;
head = nextNode;
}
return previous;
}
}
2、Remove Nth Node From End of List
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.
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
assert(head);
ListNode* cur=head;
ListNode* pre=head;
int step;
for(step=0;step<n;step++){
cur=cur->next;
}
if(step==n && cur==NULL){
head=head->next;
delete pre;
return head;
}
while(cur->next!=NULL){
pre=pre->next;
cur=cur->next;
}
ListNode* tmp=pre->next;
pre->next=tmp->next;
delete tmp;
return head;
}
};
3、Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
node->next = node->next->next;
}
};
4、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.
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode node(0),*p=&node;
while(l1 && l2){
if(l1->val >l2->val){
p->next=l2;
l2=l2->next;
}else{
p->next=l1;
l1=l1->next;
}
p=p->next;
}
if(l1)
p->next=l1;
else if(l2)
p->next=l2;
return node.next;
}
};
5、Palindrome Linked List
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?
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(!head||!head->next)
return true;
ListNode* fast=head;
ListNode* slow=head;
while(fast->next && fast->next->next){
fast=fast->next->next;
slow=slow->next;
}
ListNode* last=slow->next;
ListNode* pre=head;
while(last->next){
ListNode* tmp=last->next;
last->next=tmp->next;
tmp->next=slow->next;
slow->next=tmp;
}
while(slow->next){
slow=slow->next;
if(pre->val!=slow->val)
return false;
pre=pre->next;
}
return true;
}
};
6、Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head||!head->next)
return head;
ListNode* p=head->next;
ListNode* pre=head;
while(p){
//detect if the node is the same as the previous
if(pre->val==p->val){
ListNode* tmp=p;
p=p->next;
pre->next=p;
delete tmp;
continue;
}
//delete duplicated nodes
pre=pre->next;
p=p->next;
}
return head;
}
};
7、Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1.
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.
Your code should preferably run in O(n) time and use only O(1) memory.
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA=0;
int lenB=0;
ListNode* p1=headA;
ListNode* p2=headB;
while(p1){
lenA++;
p1=p1->next;
}
while(p2){
lenB++;
p2=p2->next;
}
p1=headA;
p2=headB;
while(lenA>lenB){
lenA--;
p1=p1->next;
}
while(lenA<lenB){
lenB--;
p2=p2->next;
}
while(p1 && p2){
if(p2->val==p1->val)
return p1;
p1=p1->next;
p2=p2->next;
}
return NULL;
}
};
8、Remove Linked List Elements
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
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* newhd = new ListNode(0);
newhd->next = head;
ListNode* newlist = newhd;
while(newhd!=NULL && newhd->next!=NULL){
if(newhd->next->val == val)
newhd->next = newhd->next->next;
else
newhd = newhd->next;
}
return newlist->next;
}
};