forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add C Solutions for 3 problems: 141-Linked-List-Cycle.c, 206-Reverse-…
…Linked-List.c and 21-Merge-Two-Sorted-Lists
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* struct ListNode *next; | ||
* }; | ||
*/ | ||
|
||
bool Traverse(struct ListNode* slow, struct ListNode* fast) { | ||
|
||
if (slow == NULL || slow -> next == NULL) { | ||
return false; | ||
} | ||
|
||
if (fast == NULL || fast -> next == NULL) { | ||
return false; | ||
} | ||
|
||
if (slow == fast) { | ||
return true; | ||
} | ||
return Traverse(slow -> next, fast -> next -> next); | ||
} | ||
|
||
|
||
bool hasCycle(struct ListNode *head) { | ||
|
||
if (head == NULL) { | ||
return false; | ||
} | ||
|
||
return Traverse(head, head -> next); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* struct ListNode *next; | ||
* }; | ||
*/ | ||
|
||
|
||
struct ListNode* reverseList(struct ListNode* head){ | ||
|
||
if (head == NULL) { | ||
return NULL; | ||
} else if (head -> next == NULL) { | ||
return head; | ||
} | ||
|
||
struct ListNode* lastNode = reverseList(head -> next); | ||
head -> next -> next = head; | ||
head -> next = NULL; | ||
return lastNode; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* struct ListNode *next; | ||
* }; | ||
*/ | ||
|
||
|
||
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){ | ||
|
||
if (list1 == NULL && list2 == NULL) { | ||
return NULL; | ||
} else if (list1 != NULL && list2 == NULL) { | ||
return list1; | ||
} else if (list2 != NULL && list1 == NULL) { | ||
return list2; | ||
} | ||
|
||
struct ListNode* temp_node = NULL; | ||
|
||
if (list1 -> val < list2 -> val) { | ||
temp_node = list1; | ||
list1 = list1 -> next; | ||
} else { | ||
temp_node = list2; | ||
list2 = list2 -> next; | ||
} | ||
|
||
struct ListNode* root = temp_node; | ||
|
||
while((list1 != NULL) || (list2 != NULL)) { | ||
|
||
if ((list2 == NULL) || ((list1 != NULL) && (list1 -> val < list2 -> val))) { | ||
temp_node -> next = list1; | ||
list1 = list1 -> next; | ||
} else { | ||
temp_node -> next = list2; | ||
list2 = list2 -> next; | ||
} | ||
temp_node = temp_node -> next; | ||
} | ||
|
||
return root; | ||
} |