Questions about one-way linked list

Source: Internet
Author: User

1. transpose one-way linked list (that is, reverse order. Pay attention to the boundary condition of the linked list and consider empty linked list ).

# Include <stddef. h>

Struct listtype

{

Int data;

Struct listtype * next;

};

Typedef struct listtype * List;

/* Reverse the singly linked list * psll .*/

Void reverse_singly_linked_list (list * psll)

{

List H = NULL;

List P = * psll;

If (! Psll |! * Psll)

{

Return;

}

While (P)

{

List TMP = P;

P = p-> next;

TMP-> next = h;

H = TMP;

}

* Psll = h;

}

---------------------------------------------------------------------------

2. How do I find circular links in a linked list?

# Include <stddef. h>

Struct listtype

{

Int data;

Struct listtype * next;

};

Typedef struct listtype * List;

/* Check that whether there is loop in the singly linked list sll or not .*/

Int find_circle (list SLL)

{

List fast = sll;

List slow = sll;

If (null = fast)

{

Return-1;

}

While (Fast & fast-> next)

{

Fast = fast-> next;

Slow = slow-> next;

If (fast = slow)

{

Return 1;

}

}

Return 0;

}

Refer:

Http://ostermiller.org/find_loop_singly_linked_list.html

---------------------------------------------------------------------------

3. Find the element in the middle of the one-way linked list. If there are two elements, take the first one.

# Include <stddef. h>

Struct listtype

{

Int data;

Struct listtype * next;

};

Typedef struct listtype * List;

/* Find the middle element of the singly linked list SLL .*/

List find_middle (list SLL)

{

List slow = sll;

List fast = sll;

If (null = fast)

{

Return NULL;

}

While (1)

{

If (null = fast-> next | null = fast-> next)

{

Return slow;

}

Slow = slow-> next;

Fast = fast-> next;

/* Prevent that there is a loop in the linked list .*/

If (fast = slow)

{

Return NULL;

}

}

}

---------------------------------------------------------------------------

4. Delete the M-last element of a single-chain table.

For a one-way linked list (unknown length), design a time-saving and space-saving algorithm to find the M-last element in the linked list. Implement this algorithm and arrange handling measures for possible exceptions.

# Include <stddef. h>

Struct listtype

{

Int data;

Struct listtype * next;

};

Typedef struct listtype * List;

/* Find the MTH element of the singly linked list sll from the end .*/

List find_the_mth_element_from_end (list sll, int m)

{

List fast = sll;/* used for loop detecting .*/

List ahead = sll;

List after = sll;

If (null = ahead | M <= 0)

{

Return NULL;

}

While (M --)

{

If (null = ahead)

{

Return NULL;

}

Ahead = ahead-> next;

If (Fast & fast-> next)

{

Fast = fast-> next;

If (fast = ahead)

{

Return NULL;

}

}

}

While (1)

{

If (null = ahead)

{

Return after;

}

Ahead = ahead-> next;

After = after-> next;

If (Fast & fast-> next)

{

Fast = fast-> next;

If (fast = ahead)

{

Return NULL;

}

}

}

}

---------------------------------------------------------------------------

5. Given a one-way linked list head pointer, you must find the first node in the loop part of the linked list. If the Linked List is not a circular linked list, a null pointer is returned. For example, the following linked list is given:

0-> 1-> 2-> 3-> 4-> 5-> 6-> 7-> 8-> (3) loop

The pointer of Node 3 should be returned. Optimize the time and space.

Solution 1:

After one traversal, you can store the address to the hash table. The first duplicate address is required.

Time complexity O (N) and space complexity O (n ).

Solution 2:

Use the linked list as a directed graph for depth-first traversal. The first node pointing to the rollback edge is the desired solution.

Time complexity O (N) and space complexity O (n ).

Solution 3:

First, create a reverse linked list based on the linked list. As follows:

Original: 1-> 2-> 3-> 4-> 5-> 6-> 7-> 8-> (3)

Reverse Order: 1-> 2-> 3-> 8-> 7-> 6-> 5-> 4-> (3)

Then, starting from the two linked list head pointers, finding the node with different next pointers is the final target.

Time complexity O (N) and space complexity O (n ).

Solution 4:

Use two pointers with step sizes of 1 and 2 to move forward. After the first encounter, move forward and stop when the second encounter. Write down the steps from the first meeting to the Second Meeting. The step size is 1.
The number of steps passed by the pointer S, then s is the length of the ring. Then we use two pointers, one in the chain head, one in the second position after the chain head, and the other in step 1
Forward, judge whether two pointers are equal, if it is the starting position of the ring.

Time complexity O (N) and space complexity O (1 ).

Solution 5:

Use two pointers with step sizes of 1 and 2 to move forward. After the first encounter, move forward and stop when the second encounter. Write down the steps from the first meeting to the Second Meeting. The step size is 1.
The number of steps passed by the pointer S, then s is the length of the ring. Then we use two pointers, one in the chain head, one in the second position after the chain head, and the other in step 1
Forward, judge whether two pointers are equal, if it is the starting position of the ring.

Time complexity O (N) and space complexity O (1 ).

Solution 6: (similar to solution 5, it is better than solution 5 because the constant factor is small)

Use two pointers with steps 1 and 2 respectively. After the first encounter, place a pointer to the head of the linked list. Then, make the two pointer step values 1 and move forward at the same time. This node is the starting position of the ring.

Time complexity O (N) and space complexity O (1 ).

The C Implementation of Solution 6 is given below.

# Include <stddef. h>

Struct listtype

{

Int data;

Struct listtype * next;

};

Typedef struct listtype * List;

/*

* Find the head node of the loop in the singly linked list SLL.

* If there is not a loop in sll, null is return.

*/

List find_head_of_loop (list SLL)

{

List slow = sll;

List fast = sll;

If (null = fast)

{

Return NULL;

}

While (1)

{

If (null = fast-> next | null = fast-> next)

{

Return NULL;

}

Slow = slow-> next;

Fast = fast-> next;

If (fast = slow)

{

Slow = sll;

Break;

}

}

While (1)

{

Slow = slow-> next;

Fast = fast-> next;

If (fast = slow)

{

Return fast;

}

}

}

Refer:

Http://www.chinaunix.net/jh/23/896018.html

---------------------------------------------------------------------------

6. If a node of the one-way linked list is given, it must be deleted in constant time.

Deleting a node at constant time is definitely not feasible, but you can use false deletion to overwrite the value of the node to be deleted with the value of the next node to be deleted, then delete the next node (the next node of the node must not be blank ):

P-> DATA = p-> next-> data;

Temp = p-> next;

P-> next = temp-> next;

Free (temp );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.