# Leetcode__链表练习

244人阅读 评论(0)

#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <stdio.h>
using namespace std;

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
#define ElemType int
typedef struct ListNode {
ElemType val;
ListNode *next;
ListNode(ElemType x) : val(x), next(NULL) {}

}ListNode;

/**
*  链表翻转
*/
class Solution {
public:

return NULL;      // 如果为空返回为空

{
}

}
};

/**
*  Remove Linked List Elements,Remove all
*  elements from a linked list of integers that have value val.
*/
class SolutionRemove {
public:
ListNode* removeElements(ListNode* head, int val) {

if (head == NULL)   // 如果是空直接返回
{
return NULL;
}
/**
*  1.如果是移除第一个元素。2.移除最后的元素. 3.中间的元素
*/

{
{
}else
break;
}
{
{
}
else
{
}

// 最后一个元素
if (head == NULL&&prior->val == val)
{
prior = NULL;
break;
}
}

return res;

}
};

/**
* 创建一个长度为n的链表，
*/
{

if (n <= 0)
return NULL;
ListNode *L;
int i = 0;
int value;
ListNode *d=NULL;
L = (ListNode *)malloc(sizeof(ListNode));
L->next = NULL;
scanf_s("%d", &value);
L->val = value;
d=L;
while (i < n-1)
{
L = (ListNode *)malloc(sizeof(ListNode));
scanf_s("%d", &value);
L->val = value;
L->next = d;
d = L;
i++;
}

return L;
}

/**
*  打印链表
*/
void PrintListNode(ListNode *L)
{
if (L == NULL)
{
printf("链表为空");
}
while (L!=NULL)
{
printf("%d",L->val);
L = L->next;
}
}

/**
*  Remove Nth Node From End of List
*  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.
*/
class Solution9 {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {

return NULL;

{
return NULL;
}
// 删除倒数第二个值，用两个指针就行了
int i = 0;
while (i < n-1)
{
i++;
}
if (head->next == NULL)  // 表示n是链表的长度
{
return res->next;
}
{
prior = h1;
h1 = h1->next;
if (h1->next == NULL)  // 表示n=1
{
prior->next = NULL;
return res;
}
}
prior->next = h1->next;
return res;
}
};

/**
*  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.
*/
class Solution83 {
public:

return NULL;

while (h2!=NULL)
{
if (h1->val == h2->val)
{
h1->next = h2->next;
h2 = h2->next;
}
else
{
h1 = h1->next;
h2 = h2->next;
}
}

}
};

/**
*  Remove Duplicates from Sorted List II
*  Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
*  For example,
*  Given 1->2->3->3->4->4->5, return 1->2->5.
*  Given 1->1->1->2->3, return 2->3.
*/
class Solution82 {
public:

return NULL;
bool flag = true;

{
bool  b = true;
{
}
{
b = false;
return NULL;
}
if (!b)
{
b = true;
}
else
{
break;
}
}
ListNode *prior = head; //  保存前驱节点
{
if (node == NULL)
return h1;
{
flag = false;
node = node->next;
if (node == NULL)      // 处理最后一个值
{
prior->next = NULL;
return h1;
}

}
if (flag)
{
}else
{
flag = true;
}
}

return h1;
}
};

/**
*  Reverse a linked list from position m to n. Do it in-place and in one-pass.
*  For example:
*  Given 1->2->3->4->5->NULL, m = 2 and n = 4,
*  return 1->4->3->2->5->NULL.
*  Note:
*  Given m, n satisfy the following condition:
*  1 ≤ m ≤ n ≤ length of list.
*/
/************************************************************************/
/* 解题思路：
* 2.如果m==1表示头部要特殊处理
* 3.如果n==leng of list 也要特殊考虑
*/
/************************************************************************/
class Solution92 {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {

if (m == n)
int i = 1;
while (i < m)
{
priorM = nodeM;
nodeM = nodeM->next;
i++;
}
// 插入
ListNode *tempNode = nodeM->next;
ListNode *l;
if (m == 1)
{
ListNode *h2 = h1->next;
while (i<n)
{

priorM->next = h2;
h1 = h2;
if (h2 == NULL)
h2 = h2->next;
i++;
}

}
ListNode *dd = nodeM;
while (i<n)
{
l = tempNode->next;

tempNode->next = nodeM;
priorM->next = tempNode;
if (l == NULL)
{
dd->next = NULL;
break;
}
dd->next = l;
nodeM=tempNode ;

tempNode = l;
i++;
}
}
};

/**
*  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.
*  题目意思，没有想到重新启动一个链表啊 SB
*/
class Solution21 {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *root, *p;
root = new ListNode(-1);
p = root;
while (1)
{
if (l1 == NULL){ p->next = l2; break; }
if (l2 == NULL){ p->next = l1; break; }
if (l1->val < l2->val)
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
ListNode *temp = root->next;
delete(root);
return temp;
}
};
int main()
{
int length = 4;
PrintListNode(l);
printf("\n");

//////////////////////////////////////////////////////////////////////////
//  Solution *sol = new Solution();
//  PrintListNode(sol->reverseList(l));

//////////////////////////////////////////////////////////////////////////
//  SolutionRemove *solRemove = new SolutionRemove();
//  PrintListNode(solRemove->removeElements(l, 3));

//////////////////////////////////////////////////////////////////////////
//  Solution9 *sol9 = new Solution9;
//  PrintListNode(sol9->removeNthFromEnd(l, 3));

//////////////////////////////////////////////////////////////////////////
//  Solution83 *sol83 = new Solution83;
//  PrintListNode(sol83->deleteDuplicates(l));

//////////////////////////////////////////////////////////////////////////
//    Solution82 *sol82 = new Solution82;
//    PrintListNode(sol82->deleteDuplicates(l));

//////////////////////////////////////////////////////////////////////////
//  Solution92 *sol92 = new Solution92;
//  PrintListNode(sol92->reverseBetween(l,2,5));

//////////////////////////////////////////////////////////////////////////
//PrintListNode(l);
//printf("\n");

//Solution21 *sol21 = new Solution21;
//PrintListNode(sol21->mergeTwoLists(l, l2));

system("pause");
return 0;
}

http://blog.csdn.net/chencheng126/article/details/39029889

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：12210次
• 积分：348
• 等级：
• 排名：千里之外
• 原创：17篇
• 转载：0篇
• 译文：3篇
• 评论：4条
文章分类
阅读排行
评论排行
最新评论