链表相关

 

反转链表:

206. 反转链表

 1 class Solution {
 2 public:
 3     ListNode* reverseList(ListNode* head) {
 4         if(head == nullptr || head->next == nullptr) return head;
 5         ListNode* pre = nullptr;
 6         ListNode* cur = head;
 7         while(cur != nullptr) {
 8             ListNode* c_next = cur->next;
 9             cur->next = pre;
10             pre = cur;
11             cur = c_next;
12         }
13         return pre;
14     }
15 };

 

92. 反转链表 II

反转指定节点

 1     ListNode* reverseList(ListNode* head, ListNode* end) {
 2         //[head,end)
 3         if(head == nullptr || head->next == nullptr) return head;
 4         ListNode* pre = nullptr;
 5         ListNode* cur = head;
 6         ListNode* next = head;
 7         while(cur != end) {
 8             next = cur->next;
 9             cur->next = pre;
10             pre = cur;
11             cur = next;
12         }
13         return pre;
14     }

 

25. Reverse Nodes in k-Group(K 个一组,反转链表)

 

 

 

链表部分反转:http://www.cnblogs.com/zle1992/p/7701949.html

  空转m次,找到第m个结点,即开始翻转的链表头部,记做head;

  以head为起始结点遍历n-m次,将第i次时,将找到的结点插入到head的next中即可。

 

链表相加:http://www.cnblogs.com/zle1992/p/7707212.html

 

链表划分:http://www.cnblogs.com/zle1992/p/7727216.html

 

  分别申请两个指针p1和p2,小于x的添加到p1中,大于等于x的添加到p2中;最后,将p2链接到p1的末端即可。

 

排序链表去重:http://www.cnblogs.com/zle1992/p/6835859.html

 

 若p->next的值和p的值相等,则将p->next->next赋值给p,删除p->next;重复上述过程,直至链表尾端。

 2个链表的公共节点 :http://www.cnblogs.com/zle1992/p/6804922.html

 

82. Remove Duplicates from Sorted List II(删除有序链表中的重复元素)

posted @ 2018-02-23 21:43  乐乐章  阅读(116)  评论(0编辑  收藏  举报